工厂方法模板(java编程思想 使用匿名内部类)
2016-12-08 22:40
441 查看
package innerclasses; 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); serviceConsumer(Implementation2.factory); } }
相关文章推荐
- Java设计模式编程之工厂方法模式的使用
- Java网络编程中inputStream.available()的使用方法
- 如何在ActionScript 3.0里使用工厂模式和模板方法模式(Factory and Template Method Patterns)
- Java学习笔记27:模板Velocity使用方法
- java多线程编程之使用Synchronized关键字同步类方法
- Java:使用匿名内部类在方法内部定义并启动线程
- java编程思想3感悟(2)---每个对象都有一个接口(方法)
- Java Web开发——FreeMarker模板使用方法讲解
- [Effective Java Distilled] Item 1 考虑使用静态工厂方法来替换构造方法
- Java网络编程从入门到精通(12):使用isReachable方法探测主机是否可以连通
- Java使用反射机制优化工厂方法
- Java Servlet 编程及应用之Cookie的使用方法-Java基础-Java-编程开发
- Java网络编程从入门到精通(6):使用getCanonicalHostName方法获得主机名
- 模板方法模式(TemplateMethod Pattern)(二):java中模板方法和钩子的使用示例
- Java编程思想:面向对象的逻辑思维方法
- Java中正则表达式使用方法详解-Java基础-Java-编程开发
- Java网络编程从入门到精通 (9):使用isXxx方法判断地址类型
- Effective Java Item1:优先考虑使用静态工厂方法
- Java Web开发——FreeMarker模板使用方法讲解
- Java:使用匿名内部类在方法内部定义并启动线程