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类的创建。

[Design-Pattern]工厂模式的更多相关文章

  1. Thinking In Design Pattern——MVP模式演绎

    原文<Thinking In Design Pattern——MVP模式演绎>不知为何丢失了,故重新整理了一遍. 目录 What Is MVP Domain Model StubRepos ...

  2. 设计模式(四) Factory Pattern工厂模式

    核心: 实例化对象,实现创建者和调用者的分离 简单工厂模式 工厂方法模式 抽象工厂模式 面对对象设计的基本原则: ocp(open closed principle) 开闭原则:一个软件的实体应当对拓 ...

  3. Factory Pattern(工厂模式)

    1.工厂模式简介 工厂模式,专门负责将大量有共同接口的类实例化(用来生产对象).其定义为定义一个用于创建对象的接口,让子类决定实例化那一个类.工厂方法使一个类的实例化延迟到其子类. 工厂模式拥有以下几 ...

  4. Design Pattern - 命令模式

    一般执行一个操作的过程, 创建对象, 并调用对象的函数, 函数执行, 返回 比如下面的类图, client直接调用Receiver.action 而命令模式, 抽象出command对象, 并在comm ...

  5. Design Pattern - 访问者模式

    访问者模式 访问者模式(Visitor), 表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 这个模式相对比较复杂, 而又很少能被用上, 拿G ...

  6. 从壹开始 [ Design Pattern ] 之三 ║ 工厂模式 与 小故事

    编者按: 定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使得一个类的实例化延迟到子类. 工厂模式,是迄今为止,使用最多,最广泛的设计模式之一,它的身影几乎出现在每一个框架和个人代码之中 ...

  7. 【design pattern】工厂方法模式和抽象工厂模式

    前言 设计模式分为三大类: 创建型模式:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式: 结构型模式:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式: 行为型模式 ...

  8. [Design Pattern With Go]设计模式-工厂模式

    这次介绍的设计模式是工厂模式,这是一个比较常见的创建型模式.一般情况下,工厂模式分为三种:简单工厂.工厂方法和抽象工厂,下面慢慢举例介绍下. 简单工厂 考虑一个加密程序的应用场景,一个加密程序可能提供 ...

  9. 简单工厂设计模式(Simple Factory Design Pattern)

    [引言]最近在Youtub上面看到一个讲解.net设计模式的视频,其中作者的一个理解让我印象很深刻:所谓的设计模式其实就是运用面向对象编程的思想来解决平时代码中的紧耦合,低扩展的问题.另外一点比较有见 ...

  10. 大白话简单工厂模式 (Simple Factory Pattern)

    大白话简单工厂模式 (Simple Factory Pattern) 从买车经历说起 毕业两年,码农张小两口无法忍受挤公交,凌晨起床抢火车票的痛苦,遂计划买车.逛了多家4S店,最终定下日产某车型的轿车 ...

随机推荐

  1. form表单验证失败,阻止表单提交

    form表单验证失败,阻止表单提交 效果演示: 贴上完整代码: <!DOCTYPE html> <html lang="en"> <head> ...

  2. Web前端理论知识记录

      Web前端理论知识记录 Elena· 5 个月前 cookies,sessionStorage和localStorage的区别? sessionStorage用于本地存储一个会话(session) ...

  3. 验证reg注册表的操作

    // wRegKeyclass wRegKey{ // Operationspublic: BOOL Create(HKEY hKeyParent, LPCTSTR lpszKeyName , LPT ...

  4. Convolutional Neural Networks for Visual Recognition 7

    Two Simple Examples softmax classifier 后,我们介绍两个简单的例子,一个是线性分类器,一个是神经网络.由于网上的讲义给出的都是代码,我们这里用公式来进行推导.首先 ...

  5. freeMarker(四)——模板开发指南之模板

    学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 模板开发指南之模板 1. 总体结构 实际上用程序语言编写的程序就是模板 ...

  6. Ffmpeg移植S3C2440

    Ffmpeg移植过程: FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证.它的移植同样遵循LGPL或GPL移植方法:configure.make.make ...

  7. 使用 Python 发送短信?

    上回食行生鲜签到,我们说到怎么把签到结果发出来,于是就找到了 Twilio. Twilio 是一个位于加利福尼亚的云通信(PaaS)公司,致力于为开发者提供通讯模块的 API.由于 Twilio 为试 ...

  8. [转]七个对我最好的职业建议(精简版)--Nicholas C. Zakas

    一.不要别人点什么,就做什么 我的第一份工作,只干了8个月,那家公司就倒闭了.我问经理,接下来我该怎么办,他说: "小伙子,千万不要当一个被人点菜的厨师,别人点什么,你就烧什么.不要接受那样 ...

  9. Poj 2853,2140 Sequence Sum Possibilities(因式分解)

    一.Description Most positive integers may be written as a sum of a sequence of at least two consecuti ...

  10. 使用PowerShell创建Azure Storage的SAS Token访问Azure Blob文件

    Azure的存储包含Storage Account.Container.Blob等具体的关系如下: 我们常用的blob存储,存放在Storage Account的Container里面. 目前有三种方 ...