标签:c++ 设计模式
#include "stdafx.h" class Method { private: int _Num1; int _Num2; public: Method( int number1 = 0, int number2 = 0) :_Num1( number1), _Num2( number2) {} virtual ~Method() {} int GetNum1(){ return _Num1; } int GetNum2(){ return _Num2; } void SetNum1( int number1){ _Num1 = number1; } void SetNum2( int number2){ _Num2 = number2; } virtual int Operator() = 0; }; class ConcreteMethod1 :public Method { public: ConcreteMethod1( int number1 = 0, int number2 = 0) : Method( number1, number2) {} virtual int Operator() { return GetNum1() + GetNum2(); } }; class ConcreteMethod2 :public Method { public: ConcreteMethod2( int number1 = 0, int number2 = 0) : Method( number1, number2) {} virtual int Operator() { return GetNum1() - GetNum2(); } }; class Factory { public: static Method* Create( int style) { Method *me = NULL; switch ( style) { case 1: me = new ConcreteMethod1(); break; case 2: me = new ConcreteMethod2(); break; default: break; } return me; } };
// FactoryMethod.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Factory.h" #include <iostream> using namespace std; int _tmain (int argc , _TCHAR * argv []) { Method * me= NULL; Factory fc; me = fc.Create(1); me->SetNum1(1); me->SetNum2(2); cout << "result:" << me->Operator() << endl; getchar(); return 0; }
标签:c++ 设计模式
原文地址:http://blog.csdn.net/wwwdongzi/article/details/26289769