Interface+工厂方法设计模式
来源:互联网 发布:免费提词器软件 编辑:程序博客网 时间:2024/05/29 04:32
package interfaces;interface Service { void method1(); void method2();}interface ServiceFactory { Service getService();}class Implementation1 implements Service { Implementation1() {} // Package access public void method1() {System.out.println("Implementation1 method1");} public void method2() {System.out.println("Implementation1 method2");}}class Implementation1Factory implements ServiceFactory { public Service getService() { return new Implementation1(); }}class Implementation2 implements Service { Implementation2() {} // Package access 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 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 method1Implementation1 method2Implementation2 method1Implementation2 method2*///:~
与直接调用构造方法不同,在工厂对象上调用的是creation方法,该工厂对象会生成接口的某种具体实现。如果不使用工厂方法,就必须在某处指出要创建的Service的确切类型,以便用合适的构造方法。
如果使用匿名内部类,会更简洁些。
interface Service { void method1(); void method2();}interface ServiceFactory { Service getService();}class Implementation1 implements Service { private Implementation1() {} public void method1() {System.out.println("Implementation1 method1");} public void method2() {System.out.println("Implementation1 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("Implementation2 method1");} public void method2() {System.out.println("Implementation2 method2");} public static ServiceFactory factory = new ServiceFactory() { public Service getService() { return new Implementation2(); } };}public class Factories { public static void serviceConsumer(ServiceFactory fact) { Service s = fact.getService(); s.method1(); s.method2(); } public static void main(String[] args) { serviceConsumer(Implementation1.factory); // Implementations are completely interchangeable: serviceConsumer(Implementation2.factory); }} /* Output:Implementation1 method1Implementation1 method2Implementation2 method1Implementation2 method2*///:~
- Interface+工厂方法设计模式
- 设计模式--工厂方法
- 工厂方法设计模式
- 设计模式-工厂方法
- 【设计模式】【工厂方法】
- 设计模式 - 工厂方法
- 工厂方法设计模式
- 工厂方法设计模式
- 【设计模式】工厂方法
- 设计模式--工厂方法
- 设计模式---工厂方法
- 设计模式-工厂方法
- 工厂方法设计模式
- 工厂方法设计模式
- 设计模式-工厂方法
- 设计模式 工厂方法
- 工厂方法设计模式
- 工厂方法设计模式
- HDUOJ 2050
- POJ-1465(同余+高精度)
- Android学习不错的资源
- 虚拟机安装Oracle + 本机安装Oracle客户端进行访问
- 模拟键盘 keybord
- Interface+工厂方法设计模式
- 运行Eclipse plugin时java.lang.NoClassDefFoundError
- Linux查看目录信息命令
- Cairngorm
- HDOJ Robberies 01背包
- 生物信息学软件-1
- Struts2多个文件上传的分析及源代码说明
- 合并排序算法
- 堪比2012的六种美味毒药