设计模式之简单工厂模式,C++实现

原创http://blog.csdn.net/iuhsihsow


只上图和代码吧

设计模式之简单工厂模式,C++实现_第1张图片


#include "stdafx.h"
 #include  <windows.h>
 
 
 enum EOperator
 {
 	ADD_OPER,
 	SUB_OPER
 };
 
 class MathOperator
 {
 public:
 	MathOperator(){;}
 	virtual ~MathOperator(){;}
 
 	void SetMember(int first, int second)
 	{
 		m_nFirst = first;
 		m_nSecond = second;
 	}
 	virtual int GetResult() = 0;
 protected:
 	int m_nFirst;
 	int m_nSecond;
 };
 
 
 class AddOperator:public MathOperator
 {
 public:
 	int GetResult()
 	{
 		return m_nFirst + m_nSecond;
 	}
 };
 
 
 class SubOperator:public MathOperator
 {
 public:
 	int GetResult()
 	{
 		return m_nFirst - m_nSecond;
 	}
 };
 
 class OperatorFactory
 {
 public:
 	MathOperator *CreateOperator(EOperator eOper)
 	{
 		MathOperator *pOper = NULL;
 		switch (eOper)
 		{
 		case ADD_OPER:
 			pOper = new AddOperator();
 			break;
 		case SUB_OPER:
 			pOper = new SubOperator();
 			break;
 		}
 		return pOper;
 	}
 };
 
 
 int _tmain(int argc, _TCHAR* argv[])
 {
 	OperatorFactory OFactrory;
 	MathOperator *pOper = OFactrory.CreateOperator(ADD_OPER);
 	pOper->SetMember(2, 3);
 	int nResult = pOper->GetResult();
 	if (pOper)
 	{
 		delete pOper;
 		pOper = NULL;
 	}
 	return 0;
 }


你可能感兴趣的:(设计模式之简单工厂模式,C++实现)