I understand the abstract classes in java and, as an example of their use, the factory method is cited, and it is said that in this case we can not create a named instance of the class, which actually makes the syntax more meaningful, here is an example code:

interface Service{ void method1(); void method2(); } interface ServiceFactory{ Service getService(); } class Implementation1 implements Service{ private Implementation1(){ } public void method1(){ System.out.println("Implementetion1 print method1"); } public void method2(){ System.out.println("Implementetion1 print method2"); } public static ServiceFactory factory = new ServiceFactory(){ public Service getService(){ return new Implementation1(); } }; } class Implementation2 implements Service{ private Implementation2(){ } public void method1(){ System.out.println("Implementetion2 print method1"); } public void method2(){ System.out.println("Implementetion2 print method2"); } public static ServiceFactory factory = new ServiceFactory() { @Override public Service getService() { return new Implementation2(); } }; } class Factories{ public static void serviceConsumer(ServiceFactory fact){ Service s = fact.getService(); s.method1(); s.method2(); } } public class AbstractClass { public static void main(String[] arg){ Factories.serviceConsumer(Implementation1.factory); Factories.serviceConsumer(Implementation2.factory); } } 

It is not clear how this is better in practice than the following code:

 interface Service{ void method1(); void method2(); } class Implementation1 implements Service{ public void method1(){ System.out.println("Implementetion1 print method1"); } public void method2(){ System.out.println("Implementetion1 print method2"); } } class Implementation2 implements Service{ public void method1(){ System.out.println("Implementetion2 print method1"); } public void method2(){ System.out.println("Implementetion2 print method2"); } } class Factories{ public static void serviceConsumer(Service s){ s.method1(); s.method2(); } } public class AbstractClass { public static void main(String[] arg){ Factories.serviceConsumer(new Implementation1()); Factories.serviceConsumer(new Implementation2()); } } 

In the first case, in Factories.serviceConsumer we pass a specific field of a particular class, why not send the object immediately as in the second case, and in the second case, you need to write less code.

  • It does not matter what to transfer to the factory, it is important what comes out. - Roman C
  • but what's the gain, the result is the same code - heff
  • Glory where it says? In some cases it is not required to create an instance of the class, but this case is very rare and has nothing to do with your question. If you are going to transfer the object to the factory, then it is no longer a factory, but an injection - Roman C

1 answer 1

In the first case in Factories.serviceConsumer, we pass a specific field of a particular class, why not send the object immediately as in the second case

To transfer an object immediately, as in the second case, you must first create and initialize it, in your example it is just a constructor call, but in practice it can be a complicated procedure, with different logic for different implementations of Service , so if you give this logic to a client class ( AbstractClass ), we get a strong connection between these classes, which will continue to be expensive to maintain.

The factory method just allows you to delegate the creation of an object to another class, and the client class does not have to know anything about how to create this or that instance of the service.