创建类模式总结-简单工厂、工厂、抽象工厂、建造、原型

#pragma once
#include <Windows.h>
#include <iostream>
using namespace  std;

#define SAMPLE_FACTORY				0
#define FACTORY_MODE				0
#define ABSTRACT_FACTORY			0
#define ABSTRACT_FACTORY_OPTIMAL	0
#define BUILDER_MODE				0
#define PROTO_MODE					1

#define _INIT(x)	 x = NULL;
#define _RELEASE(x)  if (x!=NULL){delete x;x = NULL;}
//
//简单工厂模式
//在扩展时破坏了开闭原则
#if SAMPLE_FACTORY

enum{
	PRO_A= 1,
	PRO_B
};
class IProduct{
public:
	virtual void ShowPro() = 0;
};

class ProductA : public IProduct
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductA"<<endl;
	}
};

class ProductB : public IProduct
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductB"<<endl;
	}
};

class Factory{

public:
	IProduct * ShowProduct(UINT nType){
		
		IProduct *pPro = NULL;

		switch (nType)
		{
		case PRO_A:
			{
				pPro = new ProductA();
			}
			break;
		case PRO_B:
			{
				pPro = new ProductB();
			}
			break;
		default:
			break;
		}

		return pPro;
	}
};


void main()
{
	Factory *pFactory = new Factory();
	pFactory->ShowProduct(PRO_A)->ShowPro();
	pFactory->ShowProduct(PRO_B)->ShowPro();
	delete pFactory;
	pFactory = NULL;

}
#endif // _DEBUG

//
//工厂模式
//需要调用端知道调用哪一个具体的工厂类
#if FACTORY_MODE

class IProduct{
public:
	virtual void ShowPro() = 0;
};

class ProductA : public IProduct
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductA"<<endl;
	}
};

class ProductB : public IProduct
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductB"<<endl;
	}
};

class IFactory{

public:
	IFactory(){_INIT(m_pPro)}
	~IFactory(){_RELEASE(m_pPro)}
protected:
	IProduct *m_pPro;
public:
	virtual IProduct * ShowProduct() = 0;
};

class FactoryProA : public IFactory
{
public:
	FactoryProA(){m_pPro = NULL;}
	~FactoryProA(){_RELEASE(m_pPro);}
public:
	IProduct * ShowProduct()
	{
		m_pPro  = new ProductA;
		return m_pPro;
	}
};

class FactoryProB : public IFactory
{
public:
	FactoryProB(){m_pPro = NULL;}
	~FactoryProB(){_RELEASE(m_pPro);}
public:
	IProduct * ShowProduct()
	{
		m_pPro  = new ProductB;
		return m_pPro;
	}
};

void main()
{
	
	IFactory *pIFactory = new FactoryProA;
	pIFactory->ShowProduct()->ShowPro();
	delete pIFactory;
	pIFactory = new FactoryProB;
	pIFactory->ShowProduct()->ShowPro();
	delete pIFactory;
	pIFactory = NULL;
}
#endif // .
//
//抽象工厂模式
//需要调用端知道调用哪一个具体的工厂类并实现多产品的生产

#if ABSTRACT_FACTORY

class IProductMeal{
public:
	virtual void ShowPro() = 0;
};

class ProductMealA : public IProductMeal
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductMealA"<<endl;
	}
};

class ProductMealB : public IProductMeal
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductMealB"<<endl;
	}
};


class IProductFruit
{
public:
	virtual void ShowPro() = 0;
};

class ProductFruitA : public IProductFruit
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductFruitA"<<endl;
	}
};

class ProductFruitB : public IProductFruit
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductFruitB"<<endl;
	}
};

class IFactoryMealFruit{

public:
	IFactoryMealFruit(){_INIT(m_pProMeal); _INIT(m_pProFruit);}
	~IFactoryMealFruit(){_RELEASE(m_pProFruit);_RELEASE(m_pProFruit);}
protected:
	IProductMeal *m_pProMeal;
	IProductFruit *m_pProFruit;
public:
	virtual IProductMeal * ShowProductMeal() = 0;
	virtual IProductFruit * ShowProductFruit() = 0;
};

class FactoryMealAFruitB : public IFactoryMealFruit
{
public:
	FactoryMealAFruitB(){_INIT(m_pProMeal);_INIT(m_pProFruit);}
	~FactoryMealAFruitB(){_RELEASE(m_pProMeal);_RELEASE(m_pProFruit);}
public:
	IProductMeal * ShowProductMeal()
	{
		m_pProMeal  = new ProductMealA;
		return m_pProMeal;
	}

	IProductFruit * ShowProductFruit()
	{
		m_pProFruit  = new ProductFruitB;
		return m_pProFruit;
	}
};

class FactoryMealBFruitA : public IFactoryMealFruit
{
public:
	FactoryMealBFruitA(){_INIT(m_pProMeal);_INIT(m_pProFruit);}
	~FactoryMealBFruitA(){_RELEASE(m_pProMeal);_RELEASE(m_pProFruit);}
public:
	IProductMeal * ShowProductMeal()
	{
		m_pProMeal  = new ProductMealB;
		return m_pProMeal;
	}

	IProductFruit * ShowProductFruit()
	{
		m_pProFruit  = new ProductFruitA;
		return m_pProFruit;
	}
};

void main()
{

	cout<<"订单1:"<<endl;
	IFactoryMealFruit *pIFactoryMF = new FactoryMealAFruitB();
	pIFactoryMF->ShowProductMeal()->ShowPro();
	pIFactoryMF->ShowProductFruit()->ShowPro();
	delete pIFactoryMF;

	cout<<"订单2:"<<endl;
	pIFactoryMF = new FactoryMealBFruitA();
	pIFactoryMF->ShowProductMeal()->ShowPro();
	pIFactoryMF->ShowProductFruit()->ShowPro();
	delete pIFactoryMF;
	pIFactoryMF = NULL;

}

#endif // _DEBUG

//
//抽象工厂模式优化
//创建meal和fruit都使用共同的showPro的接口,现实中生成食物和生产水果都有很多共同的特性,
//因此将这两个接口类合并,从一个接口类派生

#if ABSTRACT_FACTORY_OPTIMAL

class IProduct
{
public:
	virtual void ShowPro() = 0;
};

class IProductMeal : public IProduct{
	//添加特有属性和方法
};

class ProductMealA : public IProductMeal
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductMealA"<<endl;
	}
};

class ProductMealB : public IProductMeal
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductMealB"<<endl;
	}
};


class IProductFruit : public IProduct
{
	//添加特有属性和方法

};

class ProductFruitA : public IProductFruit
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductFruitA"<<endl;
	}
};

class ProductFruitB : public IProductFruit
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductFruitB"<<endl;
	}
};

class IFactoryMealFruit{

public:
	IFactoryMealFruit(){_INIT(m_pProMeal); _INIT(m_pProFruit);}
	~IFactoryMealFruit(){_RELEASE(m_pProFruit);_RELEASE(m_pProFruit);}
protected:
	IProductMeal *m_pProMeal;
	IProductFruit *m_pProFruit;
public:
	virtual IProductMeal * ShowProductMeal() = 0;
	virtual IProductFruit * ShowProductFruit() = 0;
};

class FactoryMealAFruitB : public IFactoryMealFruit
{
public:
	FactoryMealAFruitB(){_INIT(m_pProMeal);_INIT(m_pProFruit);}
	~FactoryMealAFruitB(){_RELEASE(m_pProMeal);_RELEASE(m_pProFruit);}
public:
	IProductMeal * ShowProductMeal()
	{
		m_pProMeal  = new ProductMealA;
		return m_pProMeal;
	}

	IProductFruit * ShowProductFruit()
	{
		m_pProFruit  = new ProductFruitB;
		return m_pProFruit;
	}
};

class FactoryMealBFruitA : public IFactoryMealFruit
{
public:
	FactoryMealBFruitA(){_INIT(m_pProMeal);_INIT(m_pProFruit);}
	~FactoryMealBFruitA(){_RELEASE(m_pProMeal);_RELEASE(m_pProFruit);}
public:
	IProductMeal * ShowProductMeal()
	{
		m_pProMeal  = new ProductMealB;
		return m_pProMeal;
	}

	IProductFruit * ShowProductFruit()
	{
		m_pProFruit  = new ProductFruitA;
		return m_pProFruit;
	}
};

void main()
{

	cout<<"订单1:"<<endl;
	IFactoryMealFruit *pIFactoryMF = new FactoryMealAFruitB();
	pIFactoryMF->ShowProductMeal()->ShowPro();
	pIFactoryMF->ShowProductFruit()->ShowPro();
	delete pIFactoryMF;

	cout<<"订单2:"<<endl;
	pIFactoryMF = new FactoryMealBFruitA();
	pIFactoryMF->ShowProductMeal()->ShowPro();
	pIFactoryMF->ShowProductFruit()->ShowPro();
	delete pIFactoryMF;
	pIFactoryMF = NULL;

}
#endif // _DEBUG

//
//建造者模式---创建复杂对象时使用
//将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
//在抽象工厂模式中,要创建MealA和FruitB,但是万一忘记其一那不就没的吃了。
//因此使用建造者模式来解决

#if BUILDER_MODE

class IProduct
{
public:
	virtual void ShowPro() = 0;
};

class IProductMeal : public IProduct{
	//添加特有属性和方法
};

class ProductMealA : public IProductMeal
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductMealA"<<endl;
	}
};

class ProductMealB : public IProductMeal
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductMealB"<<endl;
	}
};


class IProductFruit : public IProduct
{
	//添加特有属性和方法

};

class ProductFruitA : public IProductFruit
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductFruitA"<<endl;
	}
};

class ProductFruitB : public IProductFruit
{
public:
	virtual void ShowPro(){
		cout<<"Create ProductFruitB"<<endl;
	}
};

class IFactoryMealFruit{

public:
	IFactoryMealFruit(){_INIT(m_pProMeal); _INIT(m_pProFruit);}
	~IFactoryMealFruit(){_RELEASE(m_pProFruit);_RELEASE(m_pProFruit);}
protected:
	IProductMeal *m_pProMeal;
	IProductFruit *m_pProFruit;
public:
	virtual IProductMeal * ShowProductMeal() = 0;
	virtual IProductFruit * ShowProductFruit() = 0;
};

class FactoryMealAFruitB : public IFactoryMealFruit
{
public:
	FactoryMealAFruitB(){_INIT(m_pProMeal);_INIT(m_pProFruit);}
	~FactoryMealAFruitB(){_RELEASE(m_pProMeal);_RELEASE(m_pProFruit);}
public:
	IProductMeal * ShowProductMeal()
	{
		m_pProMeal  = new ProductMealA;
		return m_pProMeal;
	}

	IProductFruit * ShowProductFruit()
	{
		m_pProFruit  = new ProductFruitB;
		return m_pProFruit;
	}
};

class FactoryMealBFruitA : public IFactoryMealFruit
{
public:
	FactoryMealBFruitA(){_INIT(m_pProMeal);_INIT(m_pProFruit);}
	~FactoryMealBFruitA(){_RELEASE(m_pProMeal);_RELEASE(m_pProFruit);}
public:
	IProductMeal * ShowProductMeal()
	{
		m_pProMeal  = new ProductMealB;
		return m_pProMeal;
	}

	IProductFruit * ShowProductFruit()
	{
		m_pProFruit  = new ProductFruitA;
		return m_pProFruit;
	}
};

class FactoryMealBFruitB : public IFactoryMealFruit
{
public:
	FactoryMealBFruitB(){_INIT(m_pProMeal);_INIT(m_pProFruit);}
	~FactoryMealBFruitB(){_RELEASE(m_pProMeal);_RELEASE(m_pProFruit);}
public:
	IProductMeal * ShowProductMeal()
	{
		m_pProMeal  = new ProductMealB;
		return m_pProMeal;
	}

	IProductFruit * ShowProductFruit()
	{
		m_pProFruit  = new ProductFruitB;
		return m_pProFruit;
	}
};

//抽象建造者指挥者类
class IBuilderDirector{
public:
	IBuilderDirector(){}
	IBuilderDirector(IFactoryMealFruit *pFactoryMealFruit){this->m_pFactoryMealFruit = pFactoryMealFruit;}
	~IBuilderDirector(){}

protected:

	IFactoryMealFruit *m_pFactoryMealFruit;
public:
	virtual void showProuct()=0;
};

//具体建造者指挥者类
class BuildDirector : public IBuilderDirector{
public:
	BuildDirector(IFactoryMealFruit *pFactoryMealFruit){this->m_pFactoryMealFruit = pFactoryMealFruit;}
	~BuildDirector(){}

public:
	virtual void showProuct(){
		m_pFactoryMealFruit->ShowProductFruit()->ShowPro();
		m_pFactoryMealFruit->ShowProductMeal()->ShowPro();
	}

};

void main()
{

	cout<<"订单1:"<<endl;
	IFactoryMealFruit *pIFactoryMF = new FactoryMealAFruitB();
	IBuilderDirector *pIBuilder = new BuildDirector(pIFactoryMF);
	pIBuilder->showProuct();
	delete pIFactoryMF;
	delete pIBuilder;	

	cout<<"订单2:"<<endl;
	pIFactoryMF = new FactoryMealBFruitA();
	pIBuilder = new BuildDirector(pIFactoryMF);
	pIBuilder->showProuct();
	delete pIFactoryMF;
	pIFactoryMF = NULL;

	cout<<"订单3:"<<endl;
	pIFactoryMF = new FactoryMealBFruitB();
	pIBuilder = new BuildDirector(pIFactoryMF);
	pIBuilder->showProuct();
	delete pIFactoryMF;
	pIFactoryMF = NULL;

}
#endif // _DEBUG
//
//原型模式
//原型抽象类定义clone接口
//从抽象类派生具体类,实现深拷贝构造和clone方法

#if PROTO_MODE

class IMachine{
public:
		
	IMachine(){cout<<"IMachine"<<endl;}
	~IMachine(){cout<<"~IMachine"<<endl;}
public:
	virtual IMachine* clone() = 0;
	virtual void ShowName() = 0;
	virtual void SetName(char *NewName) = 0 ;
protected:
	char *m_pMachineName;	
};

class MachineComputer : public IMachine
{
public:
	MachineComputer(char *name){

		if (name == NULL)
		{
			m_pMachineName = new char[1];
			m_pMachineName[0] = '\0';
		}else
		{
			m_pMachineName = new char[strlen(name)+1];
			ZeroMemory(m_pMachineName,sizeof(char)*(strlen(name)+1));
			strcpy_s(m_pMachineName,sizeof(char)*(strlen(name)+1),name);
		}

	}
	~MachineComputer(){
		cout<<"~MachineComputer"<<endl;
	}
	MachineComputer(const MachineComputer & mc){
		this->m_pMachineName = new char[strlen(mc.m_pMachineName)+1];
		strcpy_s(this->m_pMachineName,strlen(mc.m_pMachineName)+1,mc.m_pMachineName);

	}
public:
	virtual IMachine* clone(){

		return new MachineComputer(*this);
	}
	virtual void ShowName() {
		cout<<"m_pMachineName : "<<m_pMachineName<<endl;
	}

	virtual void SetName(char *NewName){
		if (NewName== NULL)
		{
			delete []m_pMachineName;
			m_pMachineName = NULL;

			m_pMachineName = new char[1];
			m_pMachineName[0] = '\0';
		}else
		{
			delete []m_pMachineName;
			m_pMachineName = NULL;

			m_pMachineName = new char[strlen(NewName)+1];
			strcpy_s(m_pMachineName,strlen(NewName)+1,NewName);
		}

	}

};


void main()
{

	IMachine *pMachine = new MachineComputer("world");
	pMachine->ShowName();
	

	IMachine *pMachine2 = pMachine->clone();
	pMachine2->ShowName();
	
	pMachine->SetName("pMachine set world to hello");
	pMachine->ShowName();

	delete pMachine;
	delete pMachine2;
	pMachine2 = pMachine = NULL;
}

#endif // _DEBUG



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值