策略模式

670人阅读 评论(2) 收藏 举报

先看一下策略模式的UML类图:

 

从类图可以看出,策略模式基本和简单工厂模式没什么区别,从我的理解他们两个最大的区别就是:简单工厂模式是实现对象的多样性,而策略模式适合类中的成员以方法为主; 简单工厂模式只能解决对象创建问题,对于经常变动的算法应使用策略模式。

 

放代码看看吧:

Cpp代码 复制代码
  1. //策略基类   
  2. class COperation   
  3. {   
  4. public:   
  5.     int m_nFirst;   
  6.     int m_nSecond;   
  7.     virtual double GetResult()   
  8.     {   
  9.         double dResult=0;   
  10.         return dResult;   
  11.     }   
  12. };   
  13.   
  14. //策略具体类—加法类   
  15. class AddOperation : public COperation   
  16. {   
  17. public:   
  18.     AddOperation(int a,int b)   
  19.     {   
  20.         m_nFirst=a;   
  21.         m_nSecond=b;   
  22.     }   
  23.     virtual double GetResult()   
  24.     {   
  25.         return m_nFirst+m_nSecond;   
  26.     }   
  27. };   
  28.   
  29. class Context   
  30. {   
  31. private:   
  32.     COperation* op;   
  33. public:   
  34.         Context(COperation* temp)   
  35.     {   
  36.         op=temp;   
  37.     }   
  38.     double GetResult()   
  39.     {   
  40.         return op->GetResult();   
  41.     }   
  42. };   
  43.   
  44. //客户端   
  45. int main()   
  46. {   
  47.     int a,b;   
  48.     char c;   
  49.     cin>>a>>b;   
  50.   cout<<”请输入运算符:;   
  51.     cin>>c;   
  52.     switch(c)   
  53.     {   
  54.     case ‘+’:   
  55.         Context *context=new Context(new AddOperation(a,b));   
  56.         cout<<context->GetResult()<<endl;   
  57.             break;   
  58.    default:   
  59.             break;   
  60.      }   
  61.      return 0;   
  62. }  
  1. //策略基类   
  2. class COperation  
  3. {  
  4. public:  
  5.     int m_nFirst;  
  6.     int m_nSecond;  
  7.     virtual double GetResult()  
  8.     {  
  9.         double dResult=0;  
  10.         return dResult;  
  11.     }  
  12. };  
  13.   
  14. //策略具体类—加法类   
  15. class AddOperation : public COperation  
  16. {  
  17. public:  
  18.     AddOperation(int a,int b)  
  19.     {  
  20.         m_nFirst=a;  
  21.         m_nSecond=b;  
  22.     }  
  23.     virtual double GetResult()  
  24.     {  
  25.         return m_nFirst+m_nSecond;  
  26.     }  
  27. };  
  28.   
  29. class Context  
  30. {  
  31. private:  
  32.     COperation* op;  
  33. public:  
  34.         Context(COperation* temp)  
  35.     {  
  36.         op=temp;  
  37.     }  
  38.     double GetResult()  
  39.     {  
  40.         return op->GetResult();  
  41.     }  
  42. };  
  43.   
  44. //客户端   
  45. int main()  
  46. {  
  47.     int a,b;  
  48.     char <SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">c</SPAN></SPAN>;  
  49.     cin>>a>>b;  
  50.   cout<<”请输入运算符:;  
  51.     cin>><SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">c</SPAN></SPAN>;  
  52.     switch(<SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">c</SPAN></SPAN>)  
  53.     {  
  54.     case ‘+’:  
  55.         Context *context=new Context(new AddOperation(a,b));  
  56.         cout<<context->GetResult()<<endl;  
  57.             break;  
  58.    default:  
  59.             break;  
  60.      }  
  61.      return 0;  
  62. }  
//策略基类
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里面添加相关的分类。我想到这里,大家也看到了策略方法的缺点 将对操作的判断全部放在了客户端,增加了客户的任务。

   大家知道,简单工厂模式正好是把判断操作都集中到工厂类里,于是可以想到将两个模式结合,就出现了下面的模式----策略与工厂结合模式,代码在上面代码的基础上修改:

Cpp代码 复制代码
  1. class Context   
  2. {   
  3. private:   
  4.     COperation* op;   
  5. public:   
  6.     Context(char cType)   
  7.     {   
  8.             switch (cType)   
  9.             {   
  10.         case '+':   
  11.             op=new AddOperation(3,8);   
  12.             break;   
  13.         default:   
  14.             op=new AddOperation();   
  15.             break;   
  16.              }   
  17.     }   
  18.     double GetResult()   
  19.     {   
  20.         return op->GetResult();   
  21.     }   
  22. };   
  23. //客户端   
  24. int main()   
  25. {   
  26.     int a,b;   
  27.     cin>>a>>b;   
  28.     Context *test=new Context('+');   
  29.     cout<<test->GetResult()<<endl;   
  30.     return 0;   
  31. }  
  1. class Context  
  2. {  
  3. private:  
  4.     COperation* op;  
  5. public:  
  6.     Context(char cType)  
  7.     {  
  8.             switch (cType)  
  9.             {  
  10.         case '+':  
  11.             op=new AddOperation(3,8);  
  12.             break;  
  13.         default:  
  14.             op=new AddOperation();  
  15.             break;  
  16.              }  
  17.     }  
  18.     double GetResult()  
  19.     {  
  20.         return op->GetResult();  
  21.     }  
  22. };  
  23. //客户端   
  24. int main()  
  25. {  
  26.     int a,b;  
  27.     cin>>a>>b;  
  28.     Context *test=new Context('+');  
  29.     cout<<test->GetResult()<<endl;  
  30.     return 0;  
  31. }  

posted on 2012-06-08 10:35  很多不懂呀。。  阅读(195)  评论(0编辑  收藏  举报

导航