- //运算操作基类
- class COperation
- {
- public:
- int m_nFirst;
- int m_nSecond;
- virtual double GetResult() //返回操作结果
- {
- double dResult=0;
- return dResult;
- }
- };
- //加法
- class AddOperation : public COperation
- {
- public:
- virtual double GetResult()
- {
- return m_nFirst+m_nSecond;
- }
- };
- //减法
- class SubOperation : public COperation
- {
- public:
- virtual double GetResult()
- {
- return m_nFirst-m_nSecond;
- }
- };
- //工厂类,为了方便我用的静态函数
- class CCalculatorFactory
- {
- public:
- static COperation* Create(char cOperator);
- };
- COperation* CCalculatorFactory::Create(char cOperator)
- {
- COperation *oper;
- //在C#中可以用反射来取消判断时用的switch,在C++中用什么呢?我觉得是RTTI,后面可以讲到
- switch (cOperator)
- {
- case '+':
- oper=new AddOperation();
- break;
- case '-':
- oper=new SubOperation();
- break;
- default:
- oper=new AddOperation();
- break;
- }
- return oper;
- }
- //客户端
- int main()
- {
- int a,b;
- cin>>a>>b;
- COperation * op=CCalculatorFactory::Create('-');
- op->m_nFirst=a;
- op->m_nSecond=b;
- cout<<op->GetResult()<<endl;
- return 0;
- }
UML图:
简单工厂模式说简单点就是在一个工场类里面,根据输入的不同的条件产生同一类的不同对象,然后根据类的多态性使对象具有多样性。