public interface Service {
void method1();
void method2();
}
public interface ServiceFactory {
Service getService();
}
public class Implemention1 implements Service {
@Override
public void method1() {
System.out.println("Implemention1.method1()");
}
@Override
public void method2() {
System.out.println("Implemention1.method2()");
}
}
public class ImplementionFactory implements ServiceFactory{
public Service getService() {
return new Implemention1();
}
}
public class Implemention2 implements Service{
@Override
public void method1() {
System.out.println("Implemention.mehod1()");
}
@Override
public void method2() {
System.out.println("Implemention.method2()");
}
}
public class ImplementionFactory2 implements ServiceFactory {
public Service getService() {
return new Implemention2();
}
}
public class Factory {
public static void serviceconsumer(ServiceFactory factory){
Service service = factory.getService();
service.method1();
service.method2();
}
public static void main(String[] args) {
Factory.serviceconsumer(new ImplementionFactory());
Factory.serviceconsumer(new ImplementionFactory2());
}
}