您的位置:首页 > 其它

设计模式实现——抽象工厂模式

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: