Hello all.
I started learning Java concepts by a book "Thinking in Java, 4th Edition". In the chapter about Interfaces I did not grasp the idea of the "Factory Method pattern". The pattern is described below :
"An interface is intended to be a gateway to multiple implementations, and a typical way to produce objects that fit the interface is the Factory Method design pattern. Instead of calling a constructor directly, you call a creation method on a factory object which produces an implementation of the interface."
//: interfaces/Factories.java
interface Service {
void method1();
void method2();
}
//define interface that works with Classes that implement Service interface
interface ServiceFactory {
Service getService();
}
class Implementation1 implements Service {
Implementation1() {}
@Override public void method1() {System.out.println("Implementation1 method1"); }
@Override public void method2() {System.out.println("Implementation1 method2"); }
}
class Implementation1Factory implements ServiceFactory {
@Override public Service getService() {
return new Implementation1();
}
}
class Implementation2 implements Service {
Implementation2() {}
@Override public void method1() {System.out.println("Implementation2 method1"); }
@Override public void method2() {System.out.println("Implementation2 method2"); }
}
class Implementation2Factory implements ServiceFactory {
@Override public Service getService() {
return new Implementation2();
}
}
public class Factories {
public static void serviceConsumer(ServiceFactory fact) {
Service s = fact.getService();
s.method1();
s.method2();
}
public static void main(String[] args) {
serviceConsumer(new Implementation1Factory());
serviceConsumer(new Implementation2Factory());
}
}
The necessity of the Factory classes is explained as
"Without the Factory Method, your code would somewhere have to specify the exact type of Service being created, so that it could call the appropriate constructor."
But it sounds strange for me, because we still specify the exact type of the Factory class (Implementation1Factory, Implementation2Factory). So from the logical perspective I currently see no difference using the following code which looks more concise:
//: interfaces/FactoriesTest.java
interface Service {
void method1();
void method2();
}
class Implementation1 implements Service {
Implementation1() {}
@Override public void method1() {System.out.println("Implementation1 method1"); }
@Override public void method2() {System.out.println("Implementation1 method2"); }
}
class Implementation2 implements Service {
Implementation2() {}
@Override public void method1() {System.out.println("Implementation2 method1"); }
@Override public void method2() {System.out.println("Implementation2 method2"); }
}
public class FactoriesTest {
public static void serviceConsumer(Service s) {
s.method1();
s.method2();
}
public static void main(String[] args) {
serviceConsumer(new Implementation1());
serviceConsumer(new Implementation2());
}
}/* Output:
Implementation1 method1
Implementation1 method2
Implementation2 method1
Implementation2 method2
*///:~
Of course, I understand that I miss something, but I can't find out what exactly. I would really appreciate if someone presents an example, where the Factory Method is indeed justified.
Thank you very much in advance.