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

工场与接口

2012-09-12 
工厂与接口接口和工厂模式interface Service{void method1()void method2()}interface ServiceFactory{S

工厂与接口

接口和工厂模式

interface Service{void method1();void method2();}interface ServiceFactory{Service getService();}class Implementation1 implements Service{Implementation1(){};public void method1() {System.out.println("Implementation1 method1");}public void method2() {System.out.println("Implementation1 method2");}}class ImplementationFactory implements ServiceFactory{public Service getService(){return new Implementation1();}}class Implementation2 implements Service{Implementation2(){}public void method1(){System.out.println("Implementation2 method1");}public void method2(){System.out.println("Implementation2 method2");}}class Implementation2Factory implements ServiceFactory{public Service getService(){return new Implementation2();}}public class Factories{public static void serviceConsume(ServiceFactory fact){Service s = fact.getService();s.method1();s.method2();}public static void main(String[] args){serviceConsume(new ImplementationFactory());serviceConsume(new Implementation2Factory());}}
?

热点排行