它定义了一系列的算法,并将每个算法封装起来,而且使他们还可以相互替换。策略模式让算法的变化不会影响到使用算法的客户。
优点:
1)简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。
2)避免程序中使用多重条件转移语句,使系统更灵活,并易于扩展。
3)遵守大部分GRASP原则和常用设计原则,高内聚、低耦合。
缺点:
1)因为每个具体策略类都会产生一个新类,所以会增加系统需要维护类的数量。
2)在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给策略模式的Context对象。
进一步优化可以将策略模式与简单工厂模式相结合,选择所用具体实现的职责也可以由Context对象承担,这就最大化的减轻了客户端的职责。
结合策略模式与简单工厂模式实现的简单收银系统代码:
Strategy.h:
1 #include<iostream>
2 #include<string>
3 #include<memory>
4 using namespace std;
5
6 //strategy抽象类,用作接口
7 class Strategy
8 {
9 public:
10 virtual double GetResult(double money) = 0;
11 virtual ~Strategy()
12 {
13 cout<<" in the destructor of Strategy"<<endl;
14 }
15 };
16 //正常结算方式
17 class CashNormal : public Strategy
18 {
19 double GetResult(double money)
20 {
21 return money;
22 }
23 ~CashNormal()
24 {
25 cout<<" in the destructor of CashNormal"<<endl;
26 }
27 };
28 //打折
29 class CashRebate : public Strategy
30 {
31 private:
32 double moneyRebate; //折扣率
33 public:
34 //构造函数
35 CashRebate(double rebate)
36 {
37 moneyRebate = rebate;
38 }
39
40 double GetRebate()
41 {
42 return moneyRebate;
43 }
44 void SetRebate(double rebate)
45 {
46 moneyRebate = rebate;
47 }
48
49 double GetResult(double money)
50 {
51 return money * moneyRebate;
52 }
53 };
54 //返现
55 class CashReturn : public Strategy
56 {
57 //返现的条件与方式
58 private:
59 double moneyCondition;
60 double moneyReturn;
61 public:
62 //构造函数
63 CashReturn(double condition, double re)
64 {
65 moneyCondition = condition;
66 moneyReturn = re;
67 }
68
69 double GetCondition()
70 {
71 return moneyCondition;
72 }
73 void SetCondition(double condition)
74 {
75 moneyCondition = condition;
76 }
77 double GetReturn()
78 {
79 return moneyReturn;
80 }
81 void SetReturn(double re)
82 {
83 moneyReturn = re;
84 }
85
86 double GetResult(double money)
87 {
88 if(money >= moneyCondition)
89 {
90 money = money - (int)(money / moneyCondition) * moneyReturn;
91 }
92 return money;
93 }
94 };
95
96 //现金收费工厂类
97 class CashFactory
98 {
99 public:
100 static Strategy* createGetResult(int type)
101 {
102 Strategy* cs;
103 switch(type)
104 {
105 case 0:
106 cs = new CashNormal();
107 break;
108 case 1:
109 cs = new CashRebate(0.8);
110 break;
111 case 2:
112 cs = new CashReturn(300,100);
113 break;
114 default:
115 break;
116 }
117 return cs;
118 }
119 };
120
121 //CashContext类 策略模式与简单工厂模式相结合
122 class CashContext
123 {
124 private:
125 Strategy* cs;
126 public:
127 CashContext(int type)
128 {
129 bool loop = true;
130 while(loop){
131 switch (type)
132 {
133 case 0:
134 cs =new CashNormal();
135 loop = false;
136 break;
137 case 1:
138 cs = new CashRebate(0.8);
139 loop = false;
140 break;
141 case 2:
142 cs = new CashReturn(300,100);
143 loop = false;
144 break;
145 default:
146 cout<<"输入有误! 请重新输入!"<<endl;
147 cin>>type;
148 break;
149 }
150 }
151 }
152
153 double GetResult(double money)
154 {
155 return cs->GetResult(money);
156 }
157 };
Strategy.cpp:
1 #include "Strategy.h"
2 void main(int argc, char *argv)
3 {
4 int type = 0;
5 double total = 0;
6 cout<<"选择收费方式:"<<endl
7 <<"0:正常方式"<<endl
8 <<"1:打折方式"<<endl
9 <<"2:返现方式"<<endl;
10 cin >> type;
11 /*
12 CashFactory cfactory;
13 Strategy *pay = cfactory.createGetResult(type);
14 cout<<"输入总的消费金额:";
15 cin >> total;
16 cout<<"应收金额为:"<<pay->GetResult(total)<<endl;
17 */
18 CashContext cs(type);
19 cout<<"输入总的消费金额:";
20 cin >> total;
21 cout<<"应收金额为:"<<cs.GetResult(total)<<endl;
22 }
来源:https://www.cnblogs.com/shellfishsplace/p/6337880.html