Java版本

 1 package interfaces;2 3 interface Service {4     void method1();5     void method2();6 } 7 8 interface ServiceFactory { 9  Service getService(); 10 } 11 12 class Implementation1 implements Service { 13  Implementation1() {} 14 public void method1() { System.out.println("Implementation1 method1"); } 15 public void method2() { System.out.println("Implementation1 method2"); } 16 } 17 18 class Implementation1Factory implements ServiceFactory { 19 public Service getService() { 20 return new Implementation1(); 21  } 22 } 23 24 class Implementation2 implements Service { 25  Implementation2() {} 26 public void method1() { System.out.println("Implementation2 method1"); } 27 public void method2() { System.out.println("Implementation2 method2"); } 28 } 29 30 class Implementation2Factory implements ServiceFactory { 31 public Service getService() { 32 return new Implementation2(); 33  } 34 } 35 36 public class Factories { 37 public static void serviceConsumer(ServiceFactory fact) { 38 Service s = fact.getService(); 39  s.method1(); 40  s.method2(); 41  } 42 public static void main(String[] args) { 43 serviceConsumer(new Implementation1Factory()); 44 serviceConsumer(new Implementation2Factory()); 45  } 46 }

C++版本

 1 #include <stdio.h>2 3 class Service {4 public:5     virtual void method1() = 0;6     virtual void method2() = 0;7 }; 8 9 class ServiceFactory { 10 public: 11 virtual Service* getService() = 0; 12 }; 13 14 class Implementation1 :public Service { 15 public: 16  Implementation1() {} 17 virtual void method1() { printf("Implementation1 method1\n"); } 18 virtual void method2() { printf("Implementation1 method2\n"); } 19 }; 20 21 class Implementation2 :public Service { 22 public: 23  Implementation2() {} 24 virtual void method1() { printf("Implementation2 method1\n"); } 25 virtual void method2() { printf("Implementation2 method2\n"); } 26 }; 27 28 class Implementation1ServiceFactory :public ServiceFactory { 29 public: 30  Implementation1ServiceFactory() {} 31 virtual Service* getService() { 32 return new Implementation1(); 33  } 34 }; 35 36 class Implementation2ServiceFactory :public ServiceFactory { 37 public: 38  Implementation2ServiceFactory() {} 39 virtual Service* getService() { 40 return new Implementation2(); 41  } 42 }; 43 44 void serviceConsumer(ServiceFactory* fact) { 45 Service* s = fact->getService(); 46 s->method1(); 47 s->method2(); 48 } 49 50 int main() 51 { 52 serviceConsumer(new Implementation1ServiceFactory()); 53 serviceConsumer(new Implementation2ServiceFactory()); 54 return 0; 55 }

Java使用匿名内部类后的版本

 1 package innerclasses;2 3 interface Service {4     void method1();5     void method2();6 } 7 8 interface ServiceFactory { 9  Service getService(); 10 } 11 12 class Implementation1 implements Service { 13 private Implementation1() {} 14 public void method1() { System.out.println("Implementation1 method1"); } 15 public void method2() { System.out.println("Implementation1 method2"); } 16 public static ServiceFactory factory = new ServiceFactory() { 17  @Override 18 public Service getService() { 19 return new Implementation1(); 20  } 21  }; 22 } 23 24 class Implementation2 implements Service { 25 private Implementation2() {} 26 public void method1() { System.out.println("Implementation2 method1"); } 27 public void method2() { System.out.println("Implementation2 method2"); } 28 public static ServiceFactory factory = new ServiceFactory() { 29  @Override 30 public Service getService() { 31 return new Implementation2(); 32  } 33  }; 34 } 35 36 public class Factories { 37 public static void serviceConsumer(ServiceFactory fact) { 38 Service s = fact.getService(); 39  s.method1(); 40  s.method2(); 41  } 42 public static void main(String[] args) { 43  serviceConsumer(Implementation1.factory); 44  serviceConsumer(Implementation2.factory); 45  } 46 }

可以看出,Java在使用了匿名内部类后,代码明显少了很多,省去了Implementation1ServiceFactory、Implementation2ServiceFactory类的创建。

转载于:https://www.cnblogs.com/brianyi/p/6565191.html

[Design-Pattern]工厂模式相关推荐

  1. Thinking In Design Pattern——工厂模式演绎

    我始终认为学习设计模式需要怀着一颗敬畏的心去探索,这一系列23种设计模式并不是一蹴而就,都是前人根据自己的经验逐渐演化出来,所以才会形成非常经典的理论.学习设计模式,我想最好的方式是根据自己的经验逐渐 ...

  2. Factory Pattern工厂模式

    源地址 工厂模式是一种创建型模式,提供了一种创建对象的最佳方法模式. 应用实例: 您需要一辆汽车,可以直接从工厂里面提货,而不用去管这辆汽车是怎么做出来的,以及这个汽车里面的具体实现. Hiberna ...

  3. Java设计模式 Design Pattern:包装模式 Decorator Pattern

    意图 Attach additional responsibilities to an object dynamically. 为一个对象动态的添加职责. Decorators provide a f ...

  4. The Singleton of Design Pattern单态模式

    1 Singleton Definition 单态模式定义        The main purpose is to gurantee that the instance of class has ...

  5. Design Pattern: Singleton 模式

    一句话概括:保证一个类仅有一个实例,并提供一个访问它的全局访问点. Singleton的英文意义是独身,也就是只有一个人,应用在物件导向语言上,通常翻译作单例:单一个实例(Instance).  很多 ...

  6. Design Pattern: Adapter 模式 - Object Adapter

    您的电脑是个旧电脑,新的滑鼠都在使用USB接口了,而您的电脑上并没有USB,而只有一个PS2接口,这时您可以使用一个USB转PS2的接头作为转换,这样您的电脑就可以使用新滑鼠了(当然您也可以使用USB ...

  7. Head First Design Pattern 读书笔记(4) 工厂模式

    2019独角兽企业重金招聘Python工程师标准>>> Head First Design Pattern 读书笔记(4) Factory Pattern 工厂模式 ##Factor ...

  8. Java工厂模式(随笔)

    目录 前言 一.三大工厂模式以及特殊工厂模式介绍 1.简单工厂模式简介(Simple Factory Pattern) 2.工厂模式简介   (Factory Pattern) 3.抽象工厂模式简介 ...

  9. 工厂模式(简单工厂模式和工厂方法模式)详解

    简单工厂模式 (Simple Factory Pattern) 工厂方法模式 (Factory Method Pattern) 工厂模式: 工厂方法模式定义一个用于创建对象的接口,让子类决定实例化哪一 ...

  10. 【Design pattern】简单工厂过渡策略模式

    把自己当做小菜来跟学<大话设计模式>,跟着故事的思路来走 简单工厂模式:实现一个计算器代码 策略模式:商场打折代码 根据大鸟和小菜的故事,一步步的完善问题的过程!

最新文章

  1. 再谈javascript图片预加载经典技术
  2. python隐藏部分代码_python隐藏类中属性的3种实现方法
  3. 进程外Session和进程内Session存储
  4. 为Eclipse plug-in(插件)创建语言包
  5. 按照顺序执行_问一个多线程的问题:如何才能保证线程有序执行?
  6. 讲一个让你们难过很久的故事吧?
  7. python电脑怎么打开任务管理器_利用Python调用Windows API,实现任务管理器功能
  8. 数据工作者必备工作技能:数据治理
  9. Thinking in Java 10.8.1 闭包与回调
  10. python对列表进行去重_Python编程常用技巧–持续更新
  11. pic单片机 c语言开发环境,《PIC单片机开发环境入门》.pdf
  12. 【系统分析师之路】第五章 复盘软件工程(软件过程改进)
  13. CPC客户端离线升级失败,不能获取updatesipo信息,可能你的软件在线更新程序没有安装
  14. PHP 如何使用Mobile Detect来判断访问网站的设备 安卓,平板,电脑
  15. 计算机制作ppt教程,ppt怎么做?手机电脑超全PPT制作教程_教你做出完整的PPT
  16. 光伏电站matlab仿真,光伏发电的MATLAB仿真.doc
  17. 教务系统选课(抢课)技巧
  18. laravel框架中Cache缓存类中的原子锁
  19. 3款养生保健粥护理肠胃保健康
  20. python colormap_Python colors.LinearSegmentedColormap方法代码示例

热门文章

  1. arm中断保护和恢复_浅谈ARM处理器的七种异常处理
  2. Java 多线程 —— 常用并发容器
  3. python闪光培训班 费用-Python tk 按钮颜色
  4. Apprentissage du français partie 3
  5. php三维数组转换二维数组,php 三维数组转二维数组(多维数组变合拼二维数组)(foreach循环 数组叠加)...
  6. python julian date_Python 的内嵌time模板翻译及说明
  7. arduinojson 转 string_安德胜工作室发来本周五嗨唱转起来第二季首秀的嘉宾剧透...
  8. java 传递bean_Java:如何将值从类/ bean传递给servlet
  9. java义一个方法,返回一组双色球票数
  10. java用switch语句根据分数输出学生等级