java内部类学习(另一种工厂模式)
通过内部类实现工厂模式。
//Service接口interface Service {void method1();void method2();}//创建Service的工厂接口interface ServiceFactory {Service getService();}//Service实现class Implementation1 implements Service {private Implementation1() {}public void method1() {System.out.println("implements1 method1");}public void method2() {System.out.println("implements1 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("implements2 method1");}public void method2() {System.out.println("implements2 method2");};public static ServiceFactory factory = new ServiceFactory(){public Service getService(){return new Implementation2();}};}class Factories {public static void serviceConsumer(ServiceFactory fact) {Service s = fact.getService();s.method1();s.method1();}public static void main(String[] args){serviceConsumer(Implementation1.factory);serviceConsumer(Implementation2.factory);}}?