设计模式之工厂模式factory 抽象工厂模式 abstract factory

工厂模式 factory pattern提供创建对象的接口.

抽象工厂模式 abstract factory pattern

分析:

FactoryPattern.h

#ifndef _FACTORYPATTERN_H_
#define _FACTORYPATTERN_H_


typedef enum{
	SALE_OBJECT_CHILD,     //儿童手机
	SALE_OBJECT_YOUNESTER, //青年人手机
	SALE_OBJECT_OLD        //老年人手机
}Saleobject;

typedef enum{
	PHONE_TYPE_FEATURE,
	PHONE_TYPE_ANDROID,
	PHONE_TYPE_WINDOWSPHONE
}PhoneType;

typedef enum{
	TABLEPC_TYPE_ANDROID,
	TABLEPC_TYPE_WINDOWSPHONE,
	TABLEPC_TYPE_IPAD,
	TABLEPC_TYPE_BADA
}TablePCType;

#define NAME_MAX_LEN	(256)
#define MIN(a, b)       ((a<=b)?(a):(b))

//手机类,为纯虚类
class Phone{
public:
	Phone();
	virtual ~Phone();//虚析构函数
	
	//使用主板,显示屏,外壳,键盘等物料来研发手机
	virtual bool developPhone(void) = 0;

protected:
	int m_nMainboard;  //主板
	int m_nLcd;        //显示屏
	int m_nShell;      //外壳
	int m_nKeyboard;   //键盘
};

/*
 *	手机有很多(派生出)很多手机,如功能手机,Android手机,WindowsPhone手机等。
 */

//功能手机
class FeaturePhone:public Phone{
public:
	FeaturePhone();

	~FeaturePhone();

	//virual关键字写不写都一样,这里都是虚函数
	//必须overide
	virtual bool developPhone(void);

protected:
	Saleobject m_eSaleobj; //手机销售面向人群
};

//Android手机
class AndroidPhone:public Phone{
public:
	AndroidPhone();
	
	~AndroidPhone();
	
	//virual关键字写不写都一样,这里都是虚函数
	//必须overide
	virtual bool developPhone(void);
	
private:
	char m_szOSName[NAME_MAX_LEN]; //操作系统名称,增加一个成员
};

//WindowsPhone手机
class WPPhone:public Phone{
public:
	WPPhone();
	
	~WPPhone();
	
	//virual关键字写不写都一样,这里都是虚函数
	//必须overide
	virtual bool developPhone(void);
	
private:
	char m_szOSName[NAME_MAX_LEN]; //增加一个成员
};

/*
 *	设计一个手机生产工厂类
 */
class MobilePhoneFactory{
public:
	MobilePhoneFactory();
	
	~MobilePhoneFactory();

	//根据要求生产手机的类型来生产手机
	const Phone * makePhone(PhoneType ephonetype);
	
	//注意生产的手机未做自动销毁处理(对象删除),自动销毁也不符合自然规律
};
#endif

分析:客户想要一部手机(儿童手机,青年人手机,老年人手机),而手机需要通过工厂生产。
在程序中表现为:要通过工厂类MobilePhoneFactory去实例化具体手机类FeaturePhone、AndroidPhone或WPPhone等。可以通过
MobilePhoneFactory *pcFactory = NULL;
pcFactory = new MobilePhoneFactory();

通过MobilePhoneFactory::makePhone即可得到想要的手机实例。
PhoneType ephonetype = PHONE_TYPE_ANDROID;
Phone *pcphone = NULL;
pcphone = (AndroidPhone *)pcFactory->makePhone(ephonetype);
pcFactory->makePhone(ephonetype);
if (NULL != pcphone)
{
   delete pcphone;
}
这样对客户来说,在整个过程中就不涉及到MobilePhoneFactory的具体子类,达到封装效果,也就减少错误修改的机会。

使用工厂方法 要注意几个角色,首先你要定义产品接口类,如上面的Phone,产品接口下有Phone接口的实现类,如FeaturePhone、AndroidPhone或WPPhone等,其次要有一个MobilePhoneFactory类,用来生成产品Phone。 

AbstractFactoryPattern.h

#ifndef _ABSTRCTORYFACTORYPATTERN_H_
#define _ABSTRCTORYFACTORYPATTERN_H_

#include "FactoryPattern.h"

typedef enum{
	BRAND_NAME_SUMSANG,
	BRAND_NAME_HTC,
	BRAND_NAME_MAX
}BrandId;

//平板电脑class
class TablePC{
public:
	TablePC();
	virtual ~TablePC();

	virtual void develpTablePC(void) = 0;

protected:
	int m_nSize;
	int m_nColor;
};

//安卓平板电脑class
class AndroidTablePC:public TablePC{
public:
	//对比起见,这里不写virtual,效果是一样的。
	/*virtual*/ void develpTablePC(void);
};

//WindowsPhone平板电脑class
class WPTablePC:public TablePC{
public:
	/*virtual*/ void develpTablePC(void);
};

//iPad平板电脑class
class iPadTablePC:public TablePC{
public:
	/*virtual*/ void develpTablePC(void);
};

//Bada平板电脑class
class BadaTablePC:public TablePC{
public:
	/*virtual*/ void develpTablePC(void);
};


/*
 *	设计一个平板电脑生产工厂类
 */
class TablePCFactory{
public:
	TablePCFactory();
	
	~TablePCFactory();

	//根据要求生产平板电脑的类型来生产平板电脑
	TablePC * makeTablePC(TablePCType etablepctype);
};

/*
 *	即能生产手机也能生产平板电脑的工厂,抽象工厂类
 */
class Phone_TablePC_Factory{
public:
	Phone_TablePC_Factory(const char *szName);
	~Phone_TablePC_Factory();

	//组装手机和平板电脑品牌,为便于实例化,引入一个品牌ID的参数,
	//实际应用中应用更复杂。
	virtual bool makeBrand(BrandId id)=0;

protected:
	char m_szBrandName[NAME_MAX_LEN];              //品牌名
	MobilePhoneFactory *m_pcPhoneFactory;          //手机生产工厂
	TablePCFactory *m_pcPadFactory;                //平板电脑生产工厂
};

//能同时生产手机和平板电脑的代工工厂A,B(如富士康,MITAC),为子类实现类
//OEMA
class OEMAFactory:public Phone_TablePC_Factory{
public:
	OEMAFactory();
	OEMAFactory(const char *szName);
	~OEMAFactory();

	virtual bool makeBrand(BrandId id);
};

//OEMB
class OEMBFactory:public Phone_TablePC_Factory{
public:
	OEMBFactory();
	OEMBFactory(const char *szName);
	~OEMBFactory();
	
	virtual bool makeBrand(BrandId id);	
};

#endif

FactoryPattern.cpp

// FactoryPattern.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "FactoryPattern.h"
#include "AbstractFactoryPattern.h"
#include <iostream>
using namespace std;

/*
 *	工厂类实现
 */
Phone::Phone()
{
	cout<<"Phone constructor"<<endl;
}

Phone::~Phone()
{
	cout<<"Phone destructor"<<endl;
}

//
FeaturePhone::FeaturePhone():Phone()
{
	cout<<"FeaturePhone constructor"<<endl;
}

FeaturePhone::~FeaturePhone()
{
	cout<<"FeaturePhone destructor"<<endl;
}

bool FeaturePhone::developPhone(){
	cout<<"develop FeaturePhone"<<endl;
	return true;
}

//
AndroidPhone::AndroidPhone():Phone()
{
	cout<<"AndroidPhone constructor"<<endl;
}

AndroidPhone::~AndroidPhone()
{
	cout<<"AndroidPhone destructor"<<endl;
}

bool AndroidPhone::developPhone(){
	cout<<"develop AndroidPhone"<<endl;
	return true;
}

//
WPPhone::WPPhone():Phone()
{
	cout<<"WPPhone constructor"<<endl;
}

WPPhone::~WPPhone()
{
	cout<<"WPPhone destructor"<<endl;
}

bool WPPhone::developPhone(){
	cout<<"develop WPPhone"<<endl;
	return true;
}

//
MobilePhoneFactory::MobilePhoneFactory()/*:m_pcPhone(NULL)*/
{
	cout<<"MobilePhoneFactory constructor"<<endl;
}

MobilePhoneFactory::~MobilePhoneFactory()
{
	cout<<"MobilePhoneFactory destructor"<<endl;
}

const Phone * MobilePhoneFactory::makePhone(PhoneType ephonetype)
{
	Phone *pPhoneInstance = NULL;
	
	switch (ephonetype)
	{
	case PHONE_TYPE_FEATURE:
		pPhoneInstance = new FeaturePhone();
		break;
	case PHONE_TYPE_ANDROID:
		pPhoneInstance = new AndroidPhone();
		break;
	case PHONE_TYPE_WINDOWSPHONE:
		pPhoneInstance = new WPPhone();
		break;

	default:
		//ASSERT(false);
		break;
	}
	
	if (NULL != pPhoneInstance)
	{
		pPhoneInstance->developPhone();
	}

	return pPhoneInstance;
}

/*
 *	抽象工厂类实现
 */
TablePC::TablePC()
{
	//
}

TablePC::~TablePC()
{
	//
}

void AndroidTablePC::develpTablePC(void){
	cout<<"AndroidTablePC develpTablePC"<<endl;
}

void WPTablePC::develpTablePC(void){
	cout<<"WPTablePC develpTablePC"<<endl;
}

void iPadTablePC::develpTablePC(void){
	cout<<"iPadTablePC develpTablePC"<<endl;
}

void BadaTablePC::develpTablePC(void){
	cout<<"BadaTablePC develpTablePC"<<endl;
}

//平板电脑生产厂
TablePCFactory::TablePCFactory()/*:m_pcTablePC(NULL)*/
{
	cout<<"TablePCFactory constructor"<<endl;
}

TablePCFactory::~TablePCFactory()
{
	cout<<"TablePCFactory destructor"<<endl;
}

TablePC *TablePCFactory::makeTablePC(TablePCType etablepctype)
{
	TablePC *ptablepc = NULL;

	switch(etablepctype)
	{
	case TABLEPC_TYPE_ANDROID:
		break;
	case TABLEPC_TYPE_WINDOWSPHONE:
		break;
	case TABLEPC_TYPE_IPAD:
		break;
	case TABLEPC_TYPE_BADA:
		break;
	}

	if (NULL != ptablepc)
	{
		ptablepc->develpTablePC();
	}

	return ptablepc;
}


Phone_TablePC_Factory::Phone_TablePC_Factory(const char *szName)
:m_pcPhoneFactory(NULL)
,m_pcPadFactory(NULL)
{
	cout<<"Phone_TablePC_Factory constructor"<<endl;
	
	memset(m_szBrandName, 0 , sizeof(char)*NAME_MAX_LEN);

	if(NULL != szName){
		strncpy(m_szBrandName, szName, MIN(NAME_MAX_LEN, strlen(szName)));
	}
}

Phone_TablePC_Factory::~Phone_TablePC_Factory()
{
	if (NULL != m_pcPhoneFactory)
	{
		delete m_pcPhoneFactory;
		m_pcPadFactory = NULL;
	}
	if (NULL != m_pcPadFactory)
	{
		delete m_pcPadFactory;
		m_pcPadFactory = NULL;
	}

	cout<<"Phone_TablePC_Factory destructor"<<endl;
}

//SumSang
OEMAFactory::OEMAFactory(const char *szName):Phone_TablePC_Factory(szName)
{
	cout<<"OEMAFactory overload constructor"<<endl;
}

OEMAFactory::OEMAFactory():Phone_TablePC_Factory(NULL)
{
	cout<<"OEMAFactory default constructor"<<endl;
}

OEMAFactory::~OEMAFactory()
{
	cout<<"OEMAFactory destructor"<<endl;
}

bool OEMAFactory::makeBrand(BrandId id)
{
	bool bResult = true;
	cout<<"OEMAFactory makeBrand"<<endl;

	switch(id)
	{
	case BRAND_NAME_SUMSANG:
		m_pcPhoneFactory = new MobilePhoneFactory();
		if (NULL != m_pcPhoneFactory)
		{
			//生产WindowsPhone手机
			PhoneType ephonetype = PHONE_TYPE_WINDOWSPHONE;
			m_pcPhoneFactory->makePhone(ephonetype);
		}
		m_pcPadFactory = new TablePCFactory();
		if (NULL != m_pcPadFactory)
		{
			//生产bada平板电脑
			TablePCType etabletype = TABLEPC_TYPE_BADA;
			m_pcPadFactory->makeTablePC(etabletype);
		}

		break;
	case BRAND_NAME_HTC:
		m_pcPhoneFactory = new MobilePhoneFactory();
		if (NULL != m_pcPhoneFactory)
		{
			//生产Android手机
			PhoneType ephonetype = PHONE_TYPE_ANDROID;
			m_pcPhoneFactory->makePhone(ephonetype);
		}
		m_pcPadFactory = new TablePCFactory();
		if (NULL != m_pcPadFactory)
		{
			//生产WindowsPhone平板电脑
			TablePCType etabletype = TABLEPC_TYPE_WINDOWSPHONE;
			m_pcPadFactory->makeTablePC(etabletype);
		}
		break;

	default:
		bResult = false;
		break;
	}

	return bResult;
}

//OEMB
OEMBFactory::OEMBFactory(const char *szName):Phone_TablePC_Factory(szName)
{
	cout<<"OEMBFactory overload constructor"<<endl;
}

OEMBFactory::OEMBFactory():Phone_TablePC_Factory(NULL)
{
	cout<<"OEMBFactory default constructor"<<endl;
}

OEMBFactory::~OEMBFactory()
{
	cout<<"OEMBFactory destructor"<<endl;
}

bool OEMBFactory::makeBrand(BrandId id)
{
	bool bResult = true;
	cout<<"OEMBFactory makeBrand"<<endl;
	
	switch(id)
	{
	case BRAND_NAME_SUMSANG:
		m_pcPhoneFactory = new MobilePhoneFactory();
		if (NULL != m_pcPhoneFactory)
		{
			//生产WindowsPhone手机
			PhoneType ephonetype = PHONE_TYPE_WINDOWSPHONE;
			m_pcPhoneFactory->makePhone(ephonetype);
		}
		m_pcPadFactory = new TablePCFactory();
		if (NULL != m_pcPadFactory)
		{
			//生产bada平板电脑
			TablePCType etabletype = TABLEPC_TYPE_BADA;
			m_pcPadFactory->makeTablePC(etabletype);
		}
		
		break;
	case BRAND_NAME_HTC:
		m_pcPhoneFactory = new MobilePhoneFactory();
		if (NULL != m_pcPhoneFactory)
		{
			//生产Android手机
			PhoneType ephonetype = PHONE_TYPE_ANDROID;
			m_pcPhoneFactory->makePhone(ephonetype);
		}
		m_pcPadFactory = new TablePCFactory();
		if (NULL != m_pcPadFactory)
		{
			//生产WindowsPhone平板电脑
			TablePCType etabletype = TABLEPC_TYPE_WINDOWSPHONE;
			m_pcPadFactory->makeTablePC(etabletype);
		}
		break;
		
	default:
		bResult = false;
		break;
	}

	return true;
}


int main(int argc, char* argv[])
{
	MobilePhoneFactory *pcFactory = NULL;
	pcFactory = new MobilePhoneFactory();

	if (NULL != pcFactory)
	{
		//当客户需要生产安卓手机时,通过MobilePhoneFactory::makePhone即可得到。
		PhoneType ephonetype = PHONE_TYPE_ANDROID;

		Phone *pcphone = NULL;
		pcphone = (AndroidPhone *)pcFactory->makePhone(ephonetype);
		pcFactory->makePhone(ephonetype);
		if (NULL != pcphone)
		{
			delete pcphone;
		}

		//如果客户需要生产iPhone手机,先从Phone派生一个iPhone类,然后在MobilePhoneFactory
		//中new出iPhone类即可。

		delete pcFactory;
	}
	cout<<"------------------------------------------"<<endl;
	// 通过OEMA生产HTC品牌
	Phone_TablePC_Factory *pcOEMFactory = NULL;
	pcOEMFactory = new OEMAFactory("HTC");
	pcOEMFactory->makeBrand(BRAND_NAME_HTC);
	delete pcOEMFactory;
	
	cout<<"------------------------------------------"<<endl;
	// 通过OEMB生产SUMSANG品牌
	pcOEMFactory = new OEMBFactory("SUMSANG");
	pcOEMFactory->makeBrand(BRAND_NAME_SUMSANG);
	delete pcOEMFactory;
	
	return 0;
}

    原文作者:jzp12
    原文地址: https://blog.csdn.net/jzp12/article/details/7666378
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞