Home java Factory Method - factory method

Factory Method – factory method

Author

Date

Category

Why do I need a factory method?

One of the producers of the factory method, as described on the link above (to watch the first answer marked with a check mark) is the creation of a plurality of clear designers.

But I still didn’t understand where these designers can be created

Suppose I have such a code:

Public Class Test {
    Public Static Void Main (String [] Args) {
      Test test = new test ();
      Developerfactory developerfactory = test.choice ("CPP");
      Developer developer = developerfactory.create ();
      developer.write ();
    }
    Public DeveloperFactory Choice (String S) {
      Switch (S) {
        Case "Java":
          Return new javadeveloperfactory ();
        Case "CPP":
          RETURN NEW CPPDEVELOPERFACTORY ();
        Default:
          RETURN NULL;
      }
    }
  }
public interface developerfactory {
  Developer Create ();
}
Public Class JavadeveloperFactory Implements DeveloperFactory {
  @Override
  Public Developer Create () {
    Return New Javadeveloper ();
  }
}
public interface developer {
  Void Write ();
}
Public Class Javadeveloper Implements Developer {
  @Override
  Public Void Write () {
    System.out.PrintLN ("Java Developer Writes Java Code");
  }
}

As I understand it, the author proposes to create static methods in classes at the JavadeveloperFactory level?

What else have the advantage of this method?


Answer 1

Creating a set of clear designers – nonsense. The meaning is not at all this. To begin with, let’s we altered your example, because in real life, as described with you, no one does. This pattern was degenerated (it was simplified), although in some way it looks exactly the way you described.

public interface developer {
  Void Write ();
  Static Developer of (String Type) {
    Switch (Type) {
      Case "Java": Return New Javadeveloper ();
      Case "Ruby": Return New RubyDeveloper ();
      Default: Throw New UnsupportedOperationException ("Not Supported For" + Type);
    }
  }
}
Public Class JavaDeveloper Implements Developer {
  @Override
  Public Void Write () {
    System.out.PrintLN ("Java Developer Writes Java Code");
  }
}
Public Class RubyDeveloper Implements Developer {
  @Override
  Public Void Write () {
    System.Out.printLN ("Ruby Developer Writes Ruby Code");
  }
}
Import java.util.arraylist;
Import java.util.list;
Public Class Test {
  Public Static Void Main (String [] Args) {
    List & lt; Developer & GT; developers = new arraylist & lt; & gt; ();
    developers.add (Developer.of ( "JAVA"));
    developers.add (Developer.of ( "RUBY"));
    developers.forEach (developer- & gt; developer.write ());
  }
}

One of the main problems of the PLO – the creation of an instance of the class. We consider it to this example. We have two Interface Developer and its implementation: JavaDeveloper, RubyDeveloper. Our task is to create an instance of a particular class and bring it to a variable type interface and is simple enough at first glance:

Developer developer = new JavaDeveloper ();

and here there are two problems: 1) In which item to create this instance of the class, 2) of the implementation, we currently have two, therefore, the choice of a specific implementation of the Developer interface depends on some conditions, therefore, it is necessary then write a conditional operator.
In the example described, a specific instance of the class is created depending on the string variable, which is transmitted in the parameters. So it may be if you create a specific instance of the class depending on the parameter that is transmitted in the request to your API. However, on what condition to create an object is absolutely not important, it is used simply for an example (this is not specified by the pattern).

Now consider the Test class, call it client code, because in this class we want to use the objects of our Javadeveloper and RubyDeveloper classes. Often, the specified condition and the creation of a class instance is written in the client code (in our case, this is a TEST class). And this is the most fatal error. Now it remains to understand why. In fact, everything is simple. Imagine that JavaDeveloper and RubyDeveloper objects must be used in the same TEST class, but in 50 classes. This means that you have a huge amount of duplicate code, because in each of them you wrote conditional operators.
Moreover, now the Test class along with the remaining 50 classes have links to the real Javadeveloper and RubyDeveloper classes, because you have written the condition and developer developer = new javadeveloper () everywhere. This violates Low Coupling, which means that the changes or the deletion of any of the implementations (Javadeveloper or RubyDeveloper) will entail the rewriting of the Test class and 50 other classes. And now imagine that the conditions for creating instances of these classes have changed. You need to rewrite the Test class and 50 other classes. And now your application has expanded and you need to add JavaScript developers. What, change Test and 50 other classes again?

In the case of an example that I brought, the Test class does not know anything about Javadeveloper and RubyDeveloper classes, it has a link only to the Developer interface, which is assigned to create instances of classes. Now the entire client code will only have links only to the Developer interface, which means that to add a new developer, exactly as changes in the creation of instance of classes, or delete any of the implementations it is necessary to change only one Developer interface method. It does not affect the client code.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions