[Design-Pattern]工厂模式
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]工厂模式的更多相关文章
- Thinking In Design Pattern——MVP模式演绎
原文<Thinking In Design Pattern——MVP模式演绎>不知为何丢失了,故重新整理了一遍. 目录 What Is MVP Domain Model StubRepos ...
- 设计模式(四) Factory Pattern工厂模式
核心: 实例化对象,实现创建者和调用者的分离 简单工厂模式 工厂方法模式 抽象工厂模式 面对对象设计的基本原则: ocp(open closed principle) 开闭原则:一个软件的实体应当对拓 ...
- Factory Pattern(工厂模式)
1.工厂模式简介 工厂模式,专门负责将大量有共同接口的类实例化(用来生产对象).其定义为定义一个用于创建对象的接口,让子类决定实例化那一个类.工厂方法使一个类的实例化延迟到其子类. 工厂模式拥有以下几 ...
- Design Pattern - 命令模式
一般执行一个操作的过程, 创建对象, 并调用对象的函数, 函数执行, 返回 比如下面的类图, client直接调用Receiver.action 而命令模式, 抽象出command对象, 并在comm ...
- Design Pattern - 访问者模式
访问者模式 访问者模式(Visitor), 表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 这个模式相对比较复杂, 而又很少能被用上, 拿G ...
- 从壹开始 [ Design Pattern ] 之三 ║ 工厂模式 与 小故事
编者按: 定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使得一个类的实例化延迟到子类. 工厂模式,是迄今为止,使用最多,最广泛的设计模式之一,它的身影几乎出现在每一个框架和个人代码之中 ...
- 【design pattern】工厂方法模式和抽象工厂模式
前言 设计模式分为三大类: 创建型模式:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式: 结构型模式:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式: 行为型模式 ...
- [Design Pattern With Go]设计模式-工厂模式
这次介绍的设计模式是工厂模式,这是一个比较常见的创建型模式.一般情况下,工厂模式分为三种:简单工厂.工厂方法和抽象工厂,下面慢慢举例介绍下. 简单工厂 考虑一个加密程序的应用场景,一个加密程序可能提供 ...
- 简单工厂设计模式(Simple Factory Design Pattern)
[引言]最近在Youtub上面看到一个讲解.net设计模式的视频,其中作者的一个理解让我印象很深刻:所谓的设计模式其实就是运用面向对象编程的思想来解决平时代码中的紧耦合,低扩展的问题.另外一点比较有见 ...
- 大白话简单工厂模式 (Simple Factory Pattern)
大白话简单工厂模式 (Simple Factory Pattern) 从买车经历说起 毕业两年,码农张小两口无法忍受挤公交,凌晨起床抢火车票的痛苦,遂计划买车.逛了多家4S店,最终定下日产某车型的轿车 ...
随机推荐
- JAVA- 数据库连接池原理
第一次Java程序要在MySQL中执行一条语句,那么就必须建立一个Connection对象,代表了与MySQL数据库的连接通过直接发送你要执行的SQL语句之后,就会调用Connection.close ...
- SpringMVC 文件上传及下载
首先需要导入jar包 创建一个jsp页面 package cn.happy.Controller; import java.io.File; import javax.servlet.http.Htt ...
- 应验log4j.xml时不能找到log4j.dtd
原因分析:log4j.xml中使用log4j的DTD验证其格式的有效性"<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd&quo ...
- C++(二)— STL容器的基本用法
1.vector基本操作 关于vector简单的讲就是一个动态增长的数组,里面有一个指针指向一片连续的内存空间,当空间装不下的时候会自动申请一片更大的空间(空间配置器)将原来的数据拷贝到新的空间,然后 ...
- python 3 serial module install
/************************************************************************* * python 3 serial module ...
- js的数据格式之json
//json数据格式语法:1 数据在名称/值对中2 数据由逗号分隔3 花括号保存对象4 方括号保存数组 详情请查看:js的数据格式之json
- 游戏中的 2D 可见性
转自:http://www.gameres.com/469173.html 拖动圆点转一圈,看看玩家都能看到些什么: 这个算法也能计算出给定光源所照亮的区域.对每条光线,我们可以构建出被照亮区域的光线 ...
- html锚链接
锚点(anchor):其实就是超链接的一种,一种特殊的超链接 普通的超链接,<a href="路径"></a> 是跳转到不同的页面 而锚点,<a hr ...
- AutoMapper学习笔记
进入公司后学习到的东西,之前记录在github上 现在搬运过来 AutoMapperDemo 需要安装两个包 AutoMapper AutoMapper.Extensions.Microsoft.De ...
- BluetoothGetRadioInfo 函数
DWORD BluetoothGetRadioInfo( HANDLE hRadio, PBLUETOOTH_RADIO_INFO pRadioInfo );获取蓝牙设备的信息.参数: hRadio ...