[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店,最终定下日产某车型的轿车 ...
随机推荐
- Oracle视图的使用
--视图的语法 create [ or replace ] [NO Force | Force] View schema.view_name--视图名称 [(alias,...)inline_cons ...
- DELPHI 动态 创建和释放 多个 EDIT 控件
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...
- (转)java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)
java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明);部分资料参考网络资源 1. java向MySQL插入当前时间的四种方式 第一种:将java.util.Date ...
- python-socket2
UDP,服务端 #! /usr/bin/env python #coding=utf-8 import socket #创建socket,指定ipv4,udp类型 s = socket.socket( ...
- docker 安装过程
- linux命令学习笔记(15):tail 命令
tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件, tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新 ...
- 比线程更NB的存在
阅读目录 一 引子 二 协程介绍 三 Greenlet模块 四 Gevent模块 引子 之前我们学习了线程.进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位.按道理来 ...
- Android的缓存图片不在系统图库中显示的解决办法
Android的图库会在开机的时候扫描SD卡中的图片,视频等文件,有很多App的私有图片不想在图库中显示,就需要另外处理了. 解决办法:在缓存图片的文件夹中创建 .nomedia 文件. 1. &qu ...
- Centos6.5上的iptables
1.Centos6.5默认开启了iptables 当Centos6.5上安装了MySQL后,在远程连接它,如果出现10060的错误,说明iptables在起作用. 关闭iptables即可,sudo ...
- 基于433MHz无线串口,多发一收解决方案
一.无线发展背景 随着科学技术的飞速发展,智能家居.智慧农业.智慧城市如雨后春笋.而这些行业的发展离不开无线的应用. 传统的有线连接不仅仅是成本高,包括布线安装.维护等也是成本巨大.并且机动性也很差, ...