工厂方法模式:定义了一个创建对象的接口,由子类来决定详细实例化那个对象。工厂方法模式让类的实例化转移到子类中来推断。

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

UML图:

主要包含:

  1. Product(Page):定义了工厂类创建的对象的接口
  2. ConcreteProduct(SkillPage,EducationPage,ExperiencePage):实现了Product的详细的类
  3. Creator(Document):声明了一个工厂方法,这种方法返回一个Product类型的对象。
  4. ConcreteCreator(Report,Resume):重写工厂方法来实例化详细的Product

上面的UML是工厂方法模式一般的图例,针对一个详细的有两个ConcreteProductA,ConcreteProductB。以及它们各自工厂类ConcreteCreatorA。ConcreteCreatorB的UML图例如以下所看到的:

C++代码例如以下:

#include <stdlib.h>
#include <stdio.h>
#include <iostream> class Product
{
public:
virtual void show()=0;
}; class ConcreteProductA:public Product
{
public:
void show()
{
std::cout<<"ConcreteProductA:show"<<std::endl;
}
}; class ConcreteProductB:public Product
{
public:
void show()
{
std::cout<<"ConcreteProductB:show"<<std::endl;
} }; class Creator
{
public:
virtual Product * factoryMethod()=0; }; class ConcreteCreatorA:public Creator
{
public:
Product* factoryMethod()
{
return new ConcreteProductA();
} }; class ConcreteCreatorB:public Creator
{
public:
Product* factoryMethod()
{
return new ConcreteProductB();
} }; int main()
{
std::cout<<"工厂方法模式"<<std::endl;
Creator * creatorA=new ConcreteCreatorA;
Creator * creatorB=new ConcreteCreatorB; Product * pa=creatorA->factoryMethod();
Product* pb=creatorB->factoryMethod(); pa->show();
pb->show(); delete creatorA;
delete creatorB;
delete pa;
delete pb;
return 0;
}

測试输出:

事实上还能够一个详细的ConcreteCreator相应多个ConcreteProduct,这里以一个样例为例分析:

  1. Product为Page
  2. ConcreteProduct包含SkillPage,EducationPage,ExperiencePage
  3. Creator为Document(文档)
  4. ConcreteCreator为Report(报告文档,报告文档中有SkillPage,EducationPage),Resume(简历文档。简历文档中有SkillPage,EducationPage,ExperiencePage)

这也是一个工厂方法模式的样例

UML图为:

C++代码实现例如以下:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <list>
#include <algorithm>
using namespace std; class Page
{
public:
virtual void show()=0;
}; class SkillPage:public Page
{
public:
void show()
{
std::cout<<"SkillPage::show"<<std::endl;
}
}; class EducationPage:public Page
{
public:
void show()
{
std::cout<<"Education::show"<<std::endl;
} }; class ExperiencePage:public Page
{
public:
void show()
{
std::cout<<"Experience::show"<<std::endl;
}
}; class Document
{
public:
virtual void factoryMethod()=0;
list<Page*>& getLists()
{
return lists;
}
void print()
{
list<Page*>::iterator iter;
for(iter=lists.begin();iter!=lists.end();iter++)
(*iter)->show();
}
//注意这里要将list中的指针指向的内存删除掉,不然会造成内存泄露
virtual ~Document(){
list<Page*>::iterator iter;
for(iter=lists.begin();iter!=lists.end();iter++)
{
if(*iter)
delete *iter;
}
}
private:
list<Page*> lists;
}; class Report:public Document
{
public:
void factoryMethod()
{
getLists().push_back(new SkillPage());
getLists().push_back(new EducationPage());
}
}; class Resume:public Document
{
public:
void factoryMethod()
{
getLists().push_back(new SkillPage());
getLists().push_back(new EducationPage());
getLists().push_back(new ExperiencePage());
}
}; int main()
{
std::cout<<"详细的工厂方法模式測试"<<std::endl;
Document * report=new Report();
Document * resume=new Resume();
report->factoryMethod();
resume->factoryMethod();
std::cout<<"report print"<<std::endl;
report->print();
std::cout<<"resume print"<<std::endl;
resume->print();
return 0; }

測试输出:

设计模式之五:工厂方法模式(Factory Method)的更多相关文章

  1. 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)

    原文:乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pa ...

  2. 设计模式-03工厂方法模式(Factory Method Pattern)

    插曲.简单工厂模式(Simple Factory Pattern) 介绍工厂方法模式之前,先来做一个铺垫,了解一下简单工厂模式,它不属于 GoF 的 23 种经典设计模式,它的缺点是增加新产品时会违背 ...

  3. 【设计模式】工厂方法模式 Factory Method Pattern

    在简单工厂模式中产品的创建统一在工厂类的静态工厂方法中创建,体现了面形对象的封装性,客户程序不需要知道产品产生的细节,也体现了面向对象的单一职责原则(SRP),这样在产品很少的情况下使用起来还是很方便 ...

  4. 二十四种设计模式:工厂方法模式(Factory Method Pattern)

    工厂方法模式(Factory Method Pattern) 介绍定义一个用于创建对象的接口,让子类决定将哪一个类实例化.Factory Method使一个类的实例化延迟到其子类. 示例有SqlMes ...

  5. 设计模式之工厂方法模式(Factory Method Pattern)

    一.工厂方法模式的诞生 在读这篇文章之前,我先推荐大家读<设计模式之简单工厂模式(Simple Factory Pattern)>这篇文档.工厂方法模式是针对简单工厂模式中违反开闭原则的不 ...

  6. 设计模式之 - 工厂方法模式 (Factory Method design pattern)

    1. 模式意图:  定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 2. 别名(Virtual Constructor) 3. 结构 4. 工厂方法模式C ...

  7. 【UE4 设计模式】工厂方法模式 Factory Method Pattern 及自定义创建资源

    概述 描述 又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式,或者多态工厂(Polymorphic Factory)模式 工厂父类负责定义创建产品对象的公共接口,而工厂子类 ...

  8. 工厂方法模式-Factory Method(Java实现)

    工厂方法模式-Factory Method 工厂方法模式定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法让实例化的具体内容交给子类工厂来进行. 本文中的例子是这样的. 生产一个身份证, ...

  9. 浅谈C++设计模式之工厂方法(Factory Method)

    为什么要用设计模式?根本原因是为了代码复用,增加可维护性. 面向对象设计坚持的原则:开闭原则(Open Closed Principle,OCP).里氏代换原则(Liskov Substitution ...

  10. IOS设计模式浅析之工厂方法模式(Factory Method)

    概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口. 如何隔离出这个易变对象的变化,使得系统中“其它依赖该对象的对 ...

随机推荐

  1. 【Python-2.7】list类型

    list是Python中的一种数据类型,也就是"列表".在Python中我们可以对list类型进行插入,删除,修改等操作. ##新建list类型 >>> ball ...

  2. git 分支处理

    git 创建常用(多)分支(如:Master 主分支.Develop 分.Feature 功能分支.Release 预发布分支.Hotfix(或者Fixbug) 分支)步骤1.mkdir 项目名    ...

  3. dutacm.club_1094_等差区间_(线段树)(RMQ算法)

    1094: 等差区间 Time Limit:5000/3000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total ...

  4. HDU_3182_Hamburger Magi_状态压缩dp

    Hamburger Magi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. ThinkPHP---thinkphp拓展之空操作

    [一]概论 (1)定义 空操作指系统在找不到指定操作方法的时候.会定位到空操作方法 / 控制器来执行,利用这个机制,我们可以实现错误页面的自定义和URL的优化 (2)场景 常用于错误页面的自定义 (3 ...

  6. C++ 迭代器运算符 箭头运算符->

    所有标准库容器都支持迭代器,只有少数几种才支持下标运算 迭代器运算符 运算符 作用 *iter 返回迭代器iter所指元素的引用 iter -> mem 解引用iter,并获取元素名为mem的成 ...

  7. 日本語 IME输入法(Microsoft 输入法)切换问题

    平假名 与 片假名之间的切换 按住 Ctrl + Caps Lock(平假名) 按住 Alt + Caps Lock(片假名) 另外:语言之间的切换 Alt + Shift 键 / Windows + ...

  8. python_ 学习笔记(基本数据类型)

    python3有6中标准数据类型:Number(数字).String(字符串).List(列表).Tuple(元组).Dictionary(字典).Set(集合)不可变数据:Number.String ...

  9. centos下kong源码安装

    参考资料: https://docs.konghq.com/install/source/ 环境准备:操作系统 centeros7.3 1 :openssl和pcre一般系统自带,如果没有可自己安装  ...

  10. 创建sum求多元素的和

    a = [1, 2, 3] b = [4, 5, 6] def sum_super(* args): s = 0 for i in args: s += sum(i) return s # print ...