设计模式实现——抽象工厂模式
2016-09-03 18:11
169 查看
转载请注明t1234xy4原创:http://blog.csdn.net/t1234xy4/article/details/52424048
抽象工厂模式简介
抽象工厂分简单工厂、复杂工厂(其他blog看到的分法) 这是一种基于实现来划分的,其实就是一种模式。精髓部分就是通过继承、组合的方法来隐藏正真对象,将对象的实例化过程交给工厂来做。
作用:
作用一:抽象是用来创建对象,减少类对象个数,可以防止类过度膨胀。
例如一个工厂可以生产N中产品,M个工厂就可以生产N*M种产品。如果每一种产品用一个类来设计,那么需要M*N个类,但是我们通过抽象工厂只需要M+N个类。
作用二:
从设计模式的迪米特原则(最少知道原则)来说,一旦类成爆炸型增长(就是很多很对相同的类,例如各种各样的产品类),对其他编程人员来说需要知道的类就特别多,但是抽象工厂可以简化,从而让使用人员知道更少的接口。
在《设计模式:可复用面向对象软件的基础》(P58页)给出了一个类图,很是容易理解。
具体实现实例
生产电脑的整个过程中,可以定义为一个抽象工厂模式的例子。我们现实生活中有很多种类的电子产品,电脑、手机。每款手机都有自己的公司,若公司有联想、鸿基、华硕。那么我们可以订到6种电子产品。现在我们抽象出模型:
1、定义电子工厂模型,我们命令为ElecFactory,三个品牌就有三个具体的工厂,联想工厂LenovoFactory、鸿基工厂AcerFactory、华硕工厂AusuFactory。
2、定义产品模型,有总的电子产品为elecProduct,具体的产品有:电脑Computer,手机MobiePhone。
总的来说就有结构图为:
画的不好,讲究看吧,如果有空了再去用类图画一遍。
代码
ElecProduct类就是代码中的computerProduct类工厂抽象模型
#pragma once #include "Factory.h" #include "elecProduct.h" class ComputerFactory: public Factory { public: ComputerFactory(void); virtual elecProduct* createComputer()=0; virtual elecProduct* createMobiePhone()=0; ~ComputerFactory(void); };
电子产品的抽象模型
#pragma once class elecProduct { public: elecProduct(void); virtual ~elecProduct(void); };
具体工厂
#pragma once #include "computerfactory.h" #include "elecProduct.h" class LenovoFactory : public ComputerFactory { public: LenovoFactory(void); elecProduct* createComputer(); elecProduct* createMobiePhone(); ~LenovoFactory(void); };
对应的cpp文件实现为:
#include "StdAfx.h" #include "LenovoFactory.h" #include "Computer.h" #include "MobiePhone.h" #include "elecProduct.h" #include <iostream> LenovoFactory::LenovoFactory(void) { std::cout<<"LenovoFactory Create:"<<std::endl; } elecProduct* LenovoFactory::createComputer(){ std::cout <<"Lenovo: "; return static_cast<elecProduct *> (new Computer); } elecProduct* LenovoFactory::createMobiePhone(){ std::cout << "Lenovo: "; return static_cast<elecProduct *> (new MobiePhone); } LenovoFactory::~LenovoFactory(void) { std::cout <<"~Lenovo"<<std::endl; }
另外的两个具体工厂相同,不再重复。
具体的产品
#include "StdAfx.h" #include "Computer.h" #include <iostream> Computer::Computer(void) { std::cout <<" Computer "<<std::endl; } Computer::~Computer(void) { std::cout<<" ~ Computer "<<std::endl; }
用户Client
#include <iostream> #include "AcerFactory.h" #include "elecProduct.h" #include "AusuFactory.h" #include "LenovoFactory.h" #include "MobiePhone.h" #include "Computer.h" #include "ComputerFactory.h" using namespace std; void FactoryPatternDemo(){ ComputerFactory* _lenovoFactory = new LenovoFactory; elecProduct* _lenovoComputer = _lenovoFactory->createComputer(); elecProduct* _lenovoMobiePhone = _lenovoFactory->createMobiePhone(); ComputerFactory* _ausuFactory = new AusuFactory; elecProduct* _ausuComputer = _ausuFactory->createComputer(); elecProduct* _ausuMobiePhone = _ausuFactory->createMobiePhone(); ComputerFactory* _acerFactory = new AcerFactory; elecProduct* _acerComputer = _acerFactory->createComputer(); elecProduct* _acerMobiePhone = _acerFactory->createMobiePhone(); } int main(){ FactoryPatternDemo(); system("pause"); }
测试结果
联想手机,我们先创建联想工厂,然后用联想工厂生产手机。
代码下载
http://download.csdn.net/detail/t1234xy4/9620983相关文章推荐
- [GoF设计模式]抽象工厂模式和Command模式的C++实现
- 我所理解的设计模式(C++实现)——抽象工厂模式(Abstract Factory Pattern)
- 设计模式C#实现(五)——抽象工厂模式
- 学习php设计模式 php实现抽象工厂模式
- 设计模式实现系列-简单工厂模式-工厂模式-抽象工厂模式
- 我所理解的设计模式(C++实现)——抽象工厂模式(Abstract Factory Pattern)
- 设计模式--抽象工厂模式C++实现
- GOF23设计模式之抽象工厂模式的实现
- 学习php设计模式 php实现抽象工厂模式
- 设计模式之—抽象工厂模式(Abstract Factory)-Java实现
- 设计模式 - 抽象工厂模式(Abstract Factory) C++实现
- 设计模式学习之抽象工厂模式(实现数据访问层)
- 设计模式C++实现十一:抽象工厂模式
- 桥接模式及C++实现 C++设计模式-AbstractFactory抽象工厂模式
- 设计模式——抽象工厂模式的实现[备忘录]
- Java设计模式菜鸟系列(五)抽象工厂模式建模与实现
- php实现设计模式之 抽象工厂模式
- 解析iOS应用开发中对设计模式中的抽象工厂模式的实现
- 设计模式之 抽象工厂模式的扩展(C++实现)
- java语言实现创建型设计模式—抽象工厂模式