首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java内部类学习(另一种工场模式)

2012-11-01 
java内部类学习(另一种工厂模式)通过内部类实现工厂模式。//Service接口interface Service {void method1()

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);}}
?

热点排行