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. 创建HTML5/CSS3单页Web布局

    1. [图片] 第1步:PhotoShop ​2. [代码]第2步:index.html <!DOCTYPE html><!-- The new doctype -->< ...

  2. 十四 Django框架,中间件

    django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项目的se ...

  3. algorithm之排序算法--待解决

    简述:排序算法,参见http://www.cplusplus.com/reference/algorithm/?kw=algorithm 待解决问题:各种排序算法的实现 /* template < ...

  4. VScode 为 *.cu文件 添加高亮及c++ intelligence相关操作的方法

    问题:*.cu在VScode不能像*.cc或*.cpp一样在c++及c++ intelligence插件有关键字的高亮以及go to definition等的操作 解决方案:添加*.cu与*.cpp文 ...

  5. luogu1776宝物筛选

    多重背包问题 一开始我们的转移方程是 ;i<=n;i++) for(int j=m;j>=w[i];j--) ;k<=c[i];k++) )dp[j]=max(dp[j],dp[j- ...

  6. 1018 Public Bike Management (30)(30 分)

    时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...

  7. 【JVM】jvm垃圾回收器相关垃圾回收算法

    引用计数法[原理]--->引用计数器是经典的也是最古老的垃圾收集防范.--->实现原理:对于对象A,只要有任何一个对象引用A,则计数器加1.当引用失效时,计数器减1.只要对象A的计数器值为 ...

  8. ACM学习历程—FZU2148 Moon Game(计算几何)

    Moon Game Description Fat brother and Maze are playing a kind of special (hentai) game in the clearl ...

  9. nodejs 上传图片(服务端输出全部代码)

    下面代码,全部都是nodejs端的,不用客户端代码.也就是,选择图片的form表单以及上传完毕预览图片的html,都是由node服务端输出的. 1 启动代码:(node upload.js) var ...

  10. 有关HL7 的C# 源码

    https://github.com/OSEHRA/mdo              C# http://sourceforge.net/p/nhapi/code/HEAD/tree/NHapi20/ ...