设计模式:1.2 工厂模式
2015-08-12 01:24
295 查看
工厂模式:
一个工厂专门用来产生A型号的单核,另一个工厂专门用来生产B型号的单核。客户要做的就是找好工厂,比如要A型号的核,就找A工厂要;否则找B工厂要,不再需要告诉工厂要什么型号的处理器核了。
工厂模式缺点:每增加一种产品,需要增加一个对象的工厂。相比简单工厂,需要更多的类定义
工厂方法:
定义创建对象的接口,让子类决定实例化哪一个类。工厂方法使类的实例化延迟到其子类。
优点:增加新功能只需要增加新工厂和新产品类
没有修改的变化,只有扩展的变化。工厂方法是简单工厂的进一步抽象和推广。
缺点:将逻辑从工厂搬到客户端,想要加功能,本来改工厂类的,现在修改客户端。
main.cpp
Factory.h
FactoryA.h
FactoryB.h
FactoryA.cpp
FactoryB.cpp
SingleCore.h
SingleCoreA.h
SingleCoreB.h
SingleCoreA.cpp
SingleCoreB.cpp
一个工厂专门用来产生A型号的单核,另一个工厂专门用来生产B型号的单核。客户要做的就是找好工厂,比如要A型号的核,就找A工厂要;否则找B工厂要,不再需要告诉工厂要什么型号的处理器核了。
工厂模式缺点:每增加一种产品,需要增加一个对象的工厂。相比简单工厂,需要更多的类定义
工厂方法:
定义创建对象的接口,让子类决定实例化哪一个类。工厂方法使类的实例化延迟到其子类。
优点:增加新功能只需要增加新工厂和新产品类
没有修改的变化,只有扩展的变化。工厂方法是简单工厂的进一步抽象和推广。
缺点:将逻辑从工厂搬到客户端,想要加功能,本来改工厂类的,现在修改客户端。
main.cpp
#include <iostream> #include <stdlib.h> #include "FactoryA.h" #include "FactoryB.h" #include <memory> using namespace std; /*工厂模式缺点: 每增加一种产品,就需要增加一个对象的工厂。相比简单工厂,需要更多的类。 */ void process() { FactoryA factoryA; shared_ptr<SingleCore> ptrA = factoryA.instance(); if(ptrA) { ptrA->show(); } FactoryB factoryB; shared_ptr<SingleCore> ptrB = factoryB.instance(); if(ptrB) { ptrB->show(); } } int main(int argc,char* argv[]) { process(); system("pause"); return 0; }
Factory.h
#ifndef FACTORY_H #define FACTORY_H #include <memory> #include "SingleCore.h" class Factory { public: Factory(void); virtual ~Factory(void); virtual std::shared_ptr<SingleCore> instance()=0; }; #endif
FactoryA.h
#ifndef FACTORYA_H #define FACTORYA_H #include "factory.h" #include <memory> class FactoryA : public Factory { public: FactoryA(void); ~FactoryA(void); std::shared_ptr<SingleCore> instance(); }; #endif
FactoryB.h
#ifndef FACTORYB_H #define FACTORYB_H #include "factory.h" #include <memory> class FactoryB : public Factory { public: FactoryB(void); ~FactoryB(void); std::shared_ptr<SingleCore> instance(); }; #endif
FactoryA.cpp
#include "FactoryA.h" #include "SingleCoreA.h" FactoryA::FactoryA(void) { } FactoryA::~FactoryA(void) { } //生产A核的工厂 std::shared_ptr<SingleCore> FactoryA::instance() { std::shared_ptr<SingleCore> ptrA(new SingleCoreA); return ptrA; }
FactoryB.cpp
#include "FactoryB.h" #include "SingleCoreB.h" FactoryB::FactoryB(void) { } FactoryB::~FactoryB(void) { } std::shared_ptr<SingleCore> FactoryB::instance() { std::shared_ptr<SingleCore> ptrB(new SingleCoreB); return ptrB; }
SingleCore.h
#ifndef SINGLECORE_H #define SINGLECORE_H class SingleCore { public: SingleCore(void); virtual ~SingleCore(void); virtual void show()=0; }; #endif
SingleCoreA.h
#ifndef SINGLECOREA_H #define SINGLECOREA_H #include "singlecore.h" class SingleCoreA : public SingleCore { public: SingleCoreA(void); ~SingleCoreA(void); void show(); }; #endif
SingleCoreB.h
#ifndef SINGLECOREB_H #define SINGLECOREB_H #include "singlecore.h" class SingleCoreB : public SingleCore { public: SingleCoreB(void); ~SingleCoreB(void); void show(); }; #endif
SingleCoreA.cpp
#include "SingleCoreA.h" #include <iostream> using namespace std; SingleCoreA::SingleCoreA(void) { } SingleCoreA::~SingleCoreA(void) { } void SingleCoreA::show() { cout << "单核处理器A" << endl; }
SingleCoreB.cpp
#include "SingleCoreB.h" #include <iostream> using namespace std; SingleCoreB::SingleCoreB(void) { } SingleCoreB::~SingleCoreB(void) { } void SingleCoreB::show() { cout << "单核处理器B" << endl; }
相关文章推荐
- HDU 4321 Arcane Numbers 2 按位处理, 想法计数题
- iOS包管理工具Cocoapods的安装与使用
- HDUOJ_1875(最小生成树)(畅通工程再续)(克鲁斯卡尔和prim两种方法解)
- 黑马程序员——ios学习笔记 OC Foundation—NSArray&NSDictionary
- Ray Wenderlich的swift教程02--引用类型和值类型
- Hibernate缓存非常烦人,想要最新数据连session.clear都不好使
- 黑马程序员——ios学习笔记 OC Foundation—NSString
- Quartz2D复习(四) --- 图层CALayer和动画CAAnimation
- Scala 深入浅出实战经典 第5讲:Scala数组操作实战详解
- 黑马程序员——ios学习笔记 OC 分类
- 数据结构基础温故-7.排序
- 黑马程序员——ios学习笔记 OC 协议&Block
- F - Friends-HDU 5305
- 事件引入和本质
- golang 数组切片(一)
- 互联网电商大数据环境 ——大数飓数据分析实践培训精华笔记(五)——数据仓库维度建模实例
- context.drawImage绘制图片
- nginx服务器去掉url中的index.php 和 配置path_info
- DOS命令大全
- 输入一个int型数据,计算出该int型数据在内存中存储时1的个数。