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. SpringMVC注解示例

    1.web.xml <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-cla ...

  2. curl常用命令行总结

    curl 有时HTTP服务接口写完,需要验证下接口功能,这个使用用curl最合适了 curl 全称 command line url viewer curl www.taobao.com curl w ...

  3. linux apt-get remove如何恢复

    linux卸载或删除软件时,若不小心删除到关联的软件,如果想撤销删除操作需要在/var/log/apt/history.log中依次安装删除的软件,具体操作如下: $echo '#!/bin/bash ...

  4. 51Nod1766 树上的最远点对

    1766 树上的最远点对 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i&l ...

  5. 转 Django根据现有数据库,自动生成models模型文件

    Django引入外部数据库还是比较方便的,步骤如下 : 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自 ...

  6. HDU4699:Editor

    浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=46 ...

  7. Azure上部署Barracuda WAF集群 --- 1

    公有云上的第一层防护,一般要采用Proxy模式的安全设备. 梭子鱼的WAF是最早支持Azure China公有云的安全设备. 本文记录了在Azure上安装部署Barracuda的过程.下面就是安装部署 ...

  8. 优秀开源项目之三:高性能、高并发、高扩展性和可读性的网络服务器架构State Threads

    译文在后面. State Threads for Internet Applications Introduction State Threads is an application library ...

  9. java代码throws异常

    总结:抛出异常 package com.ds; //异常捕获 public class fdsg { private static void throwException() { try { Stri ...

  10. AD9 如何画4层pcb板

    新建的PCB文件默认的是2层板,教你怎么设置4层甚至更多层板. 在工具栏点击Design-->Layer Stack Manager.进入之后显示的是两层板,添加为4层板,一般是先点top la ...