策略模式
来源:互联网 发布:淘宝客服处理技巧 编辑:程序博客网 时间:2024/06/11 19:51
一,概念
策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。
二,策略模式的组成
1)抽象策略角色: 策略类,通常由一个接口或者抽象类实现。 2)具体策略角色:包装了相关的算法和行为。3)环境角色:持有一个策略类的引用,最终给客户端调用。
三,补充C++知识
类对象的构造顺序是这样的:
1.分配内存,调用构造函数时,隐式/显示的初始化各数据成员
2.进入构造函数后在构造函数中执行一般计算
1)类里面的任何成员变量在定义时是不能初始化的。
2)一般的数据成员可以在构造函数中初始化。
3)const数据成员必须在构造函数的初始化列表中初始化。
4)static要在类的定义外面初始化。
5)数组成员是不能在初始化列表里初始化的。
6)不能给数组指定明显的初始化。
这6条一起,说明了一个问题:C++里面是不能定义常量数组的!因为3和5的矛盾。这个事情似乎说不过去啊?没有办法,我只好转而求助于静态数据成员。
到此,我的问题解决。但是我还想趁机复习一下C++类的初始化:
1.初始化列表:CSomeClass::CSomeClass() : x(0), y(1){}
2.类外初始化:int CSomeClass::myVar=3;
3.const常量定义必须初始化,C++类里面使用初始化列表;
4.C++类不能定义常量数组。
在C++类中,必须做如下事情:
1.必须对任何const或引用类型成员以及没有默认构造函数的 类 类型 的任何成员 显示地使用初始化列表进行初始化
2.类成员在定义时是不能被初始化的。
3.类的成员初始化顺序与成员变量在构造函数中的位置选后顺序无关,至于成员变量在类中定义的先后顺序有关。
C++默认继承方式为private
C++ new 生成的对象为指针,所以new 前面的对象要声明为指针类型
四,实例
计算器简单工厂模式的精简实现
- #include <iostream>
- using namespace std;
- 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;
- }
将简单工厂模式优化为策略模式后的代码如下:
- #include <iostream>
- using namespace std;
- //策略基类
- 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;
- }
这里将策略(操作符)封装成一个Context类,通过传递操作符子对象来返回相应子对象下操作结果。
实现工厂模式和策略模式
客户端只需访问Context类,而不用知道其它任何类信息,实现了低耦合。在上例基础上,修改下面内容
- #include <iostream>
- using namespace std;
- //策略基类
- 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;
- }
- AddOperation()
- {
- m_nFirst=0;
- m_nSecond=0;
- }
- virtual double GetResult()
- {
- return m_nFirst+m_nSecond;
- }
- };
- 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()
- {
- Context *test=new Context('+');
- cout<<test->GetResult()<<endl;
- return 0;
- }