設計模式(5)--工廠模式

//5.工廠模式
//ver1
//回顧簡單工廠模式
class OperationFactory
{
public:
	static Operation createOperation(char chOper)
	{
		Operation * op = NULL;
		switch(chOper)
		{
		case '+':
			op = new OperationAdd();
			break;
		case '-':
			op = new OperationSub();
			break;
		default:
			break;
		}
		return *op;
	}
};

void main11()
{
	Operation oper;
	oper = OperationFactory::createOperation('+');
	oper.SetNumberA(1.1);
	oper.SetNumberB(2.2);
	double result = oper.GetResult();
}

//工廠模式: 
//工廠接口
class Operation
{
public:
	Operation(){}
	~Operation(){}
protected:
	double _NumberA;
	double _NumberB;
public:
	void SetNumberA(double A)
	{
		_NumberA = A;
	}
	double GetNumberA()
	{
		return _NumberA;
	}
	void SetNumberB(double B)
	{
		_NumberB = B;
	}
	double GetNumberB()
	{
		return _NumberB;
	}

	virtual double GetResult()
	{
		double result = 0;
		return result;
	}
};

class OperationAdd : public Operation
{
public:
	virtual double GetResult()
	{
		double result = _NumberA + _NumberB;
		return result;
	}
};

class OperationSub : public Operation
{
public:
	virtual double GetResult()
	{
		double result = _NumberA - _NumberB;
		return result;
	}
};

class IFactory
{
public:
	virtual Operation CreateOperation()
	{
		Operation * po = NULL;
		return *po;
	}
};

class AddFactory : public IFactory
{
public:
	virtual Operation CreateOperation()
	{
		Operation * po = new OperationAdd();
		return *po;
	}
};

class SubFactory : public IFactory
{
public:
	virtual Operation CreateOperation()
	{
		Operation * po = new OperationSub();
		return *po;
	}
};

void main1()
{
	IFactory * pif = new AddFactory();
	Operation po = pif->CreateOperation();
	po.SetNumberA(1.1);
	po.SetNumberB(2.2);
	double result = po.GetResult();
}

 

//簡單工廠模式的最大優勢在於工廠類中包含了必要的邏輯判斷,根據客戶端的選擇條件動態實例化相關的類,
//對於客戶端來講,去除了與具體產品的依賴。學習

//工廠模式,定義一個用於建立對象的接口,讓子類決定實例化哪個類。工廠方法使一個類的實例化延遲到其子類。
//工廠模式實現時,客戶端須要決定實例化哪個工廠來實現運算類,選擇判斷的問題仍是存在,也就是說,工廠方式把
//簡單工廠的內部邏輯判斷移到了客戶端代碼來進行。你想要加功能,原本是改工廠類,如今是改客戶端。對象

 

//5.工廠模式
//ver2
class LeiFeng
{
public:
	void Sweep()
	{
		//打掃
	}

	void Wash()
	{
		//洗衣
	}
};

//學習雷鋒大學生
class Undergraduate : public LeiFeng
{

};

//志願者
class Volunteer : public LeiFeng
{

};

//簡單雷鋒工廠
class SimpleFactory
{
public:
	static LeiFeng CreateLeiFeng(char cType)
	{
		LeiFeng * plf = NULL;
		switch (cType)
		{
		case '1': //大學生
			plf = new Undergraduate();
			break;
		case '2': //志願者
			plf = new Volunteer();
			break;
		default:
			break;
		}
		return *plf;
	}
};

void main21()
{
	LeiFeng stuA = SimpleFactory::CreateLeiFeng('1');
	stuA.Sweep();
	LeiFeng volA = SimpleFactory::CreateLeiFeng('2');
	volA.Wash();
}

//雷鋒工廠
class IFactory
{
public:
	//IFactory(){}
	LeiFeng CreateLeiFeng()
	{
		LeiFeng * plf = new LeiFeng();
		return *plf;
	}
};

//大學生工廠
class UndergraduateFactory : public IFactory
{
public:
	//UndergraduateFactory(){}
	LeiFeng CreateLeiFeng()
	{
		LeiFeng *plf = new Undergraduate();
		return *plf;
	}
};

//志願者工廠
class VolunteerFactory : public IFactory
{
public:
	//VolunteerFactory(){}
	LeiFeng CreateLeiFeng()
	{
		LeiFeng * plf = new Volunteer();
		return *plf;
	}
};

void main22()
{
	IFactory * pf = new UndergraduateFactory();
	LeiFeng stu = pf->CreateLeiFeng();
	stu.Sweep();
	stu.Wash();
}

 

//工廠模式客服了簡單工廠違背開放-封閉原則的缺點,又保持了封裝對象建立過程的優勢。
//缺點:每加一個產品,就須要加一個產品工廠的類,增長了額外的開發量。blog

相關文章
相關標籤/搜索