OnJava8-Examples/interfaces/Factories.java

56 lines
1.3 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: interfaces/Factories.java
import static net.mindview.util.Print.*;
interface Service {
void method1();
void method2();
}
interface ServiceFactory {
Service getService();
}
class Implementation1 implements Service {
Implementation1() {} // Package access
public void method1() {print("Implementation1 method1");}
public void method2() {print("Implementation1 method2");}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
class Implementation1Factory implements ServiceFactory {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public Service getService() {
return new Implementation1();
}
}
class Implementation2 implements Service {
Implementation2() {} // Package access
public void method1() {print("Implementation2 method1");}
public void method2() {print("Implementation2 method2");}
}
class Implementation2Factory implements ServiceFactory {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public Service getService() {
return new Implementation2();
}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
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());
// Implementations are completely interchangeable:
serviceConsumer(new Implementation2Factory());
}
} /* Output:
Implementation1 method1
Implementation1 method2
Implementation2 method1
Implementation2 method2
*///:~