2010-05-07 21:26 670人阅读 评论(2) 收藏 举报
先看一下策略模式的UML类图:
从类图可以看出,策略模式基本和简单工厂模式没什么区别,从我的理解他们两个最大的区别就是:简单工厂模式是实现对象的多样性,而策略模式适合类中的成员以方法为主; 简单工厂模式只能解决对象创建问题,对于经常变动的算法应使用策略模式。
放代码看看吧:
- //策略基类
- class COperation
- {
- public:
- int m_nFirst;
- int m_nSecond;
- virtual double GetResult()
- {
- double dResult=0;
- return dResult;
- }
- };
- //策略具体类—加法类
- class AddOperation : public COperation
- {
- public:
- AddOperation(int a,int b)
- {
- m_nFirst=a;
- m_nSecond=b;
- }
- virtual double GetResult()
- {
- return m_nFirst+m_nSecond;
- }
- };
- class Context
- {
- private:
- COperation* op;
- public:
- Context(COperation* temp)
- {
- op=temp;
- }
- double GetResult()
- {
- return op->GetResult();
- }
- };
- //客户端
- int main()
- {
- int a,b;
- char c;
- cin>>a>>b;
- cout<<”请输入运算符:;
- cin>>c;
- switch(c)
- {
- case ‘+’:
- Context *context=new Context(new AddOperation(a,b));
- cout<<context->GetResult()<<endl;
- break;
- default:
- break;
- }
- return 0;
- }
- //策略基类
- class COperation
- {
- public:
- int m_nFirst;
- int m_nSecond;
- virtual double GetResult()
- {
- double dResult=0;
- return dResult;
- }
- };
- //策略具体类—加法类
- class AddOperation : public COperation
- {
- public:
- AddOperation(int a,int b)
- {
- m_nFirst=a;
- m_nSecond=b;
- }
- virtual double GetResult()
- {
- return m_nFirst+m_nSecond;
- }
- };
- class Context
- {
- private:
- COperation* op;
- public:
- Context(COperation* temp)
- {
- op=temp;
- }
- double GetResult()
- {
- return op->GetResult();
- }
- };
- //客户端
- int main()
- {
- int a,b;
- char <SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">c</SPAN></SPAN>;
- cin>>a>>b;
- cout<<”请输入运算符:;
- cin>><SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">c</SPAN></SPAN>;
- switch(<SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">c</SPAN></SPAN>)
- {
- case ‘+’:
- Context *context=new Context(new AddOperation(a,b));
- cout<<context->GetResult()<<endl;
- break;
- default:
- break;
- }
- return 0;
- }
//策略基类
class COperation
{
public:
int m_nFirst;
int m_nSecond;
virtual double GetResult()
{
double dResult=0;
return dResult;
}
};
//策略具体类—加法类
class AddOperation : public COperation
{
public:
AddOperation(int a,int b)
{
m_nFirst=a;
m_nSecond=b;
}
virtual double GetResult()
{
return m_nFirst+m_nSecond;
}
};
class Context
{
private:
COperation* op;
public:
Context(COperation* temp)
{
op=temp;
}
double GetResult()
{
return op->GetResult();
}
};
//客户端
int main()
{
int a,b;
char c;
cin>>a>>b;
cout<<”请输入运算符:;
cin>>c;
switch(c)
{
case ‘+’:
Context *context=new Context(new AddOperation(a,b));
cout<<context->GetResult()<<endl;
break;
default:
break;
}
return 0;
}
为了方便,我这里只放了一个加法类,你可以自己继承一个减法、乘法等,然后在主函数switch里面添加相关的分类。我想到这里,大家也看到了策略方法的缺点 :将对操作的判断全部放在了客户端,增加了客户的任务。
大家知道,简单工厂模式正好是把判断操作都集中到工厂类里,于是可以想到将两个模式结合,就出现了下面的模式----策略与工厂结合模式,代码在上面代码的基础上修改:
- class Context
- {
- private:
- COperation* op;
- public:
- Context(char cType)
- {
- switch (cType)
- {
- case '+':
- op=new AddOperation(3,8);
- break;
- default:
- op=new AddOperation();
- break;
- }
- }
- double GetResult()
- {
- return op->GetResult();
- }
- };
- //客户端
- int main()
- {
- int a,b;
- cin>>a>>b;
- Context *test=new Context('+');
- cout<<test->GetResult()<<endl;
- return 0;
- }
- class Context
- {
- private:
- COperation* op;
- public:
- Context(char cType)
- {
- switch (cType)
- {
- case '+':
- op=new AddOperation(3,8);
- break;
- default:
- op=new AddOperation();
- break;
- }
- }
- double GetResult()
- {
- return op->GetResult();
- }
- };
- //客户端
- int main()
- {
- int a,b;
- cin>>a>>b;
- Context *test=new Context('+');
- cout<<test->GetResult()<<endl;
- return 0;
- }