策略模式
【1】什么是策略模式?
坊间的解释都比较拗口,而且不好理解。
所谓策略模式,先构建一个策略(即子类的实例),再利用这个具体的实例构建一个策略对象,通过调用策略对象的接口可以达到相应具体策略的结果。
【2】策略模式代码示例
代码示例:
1 /*
2 * 策略模式
3 */
4
5 #include <iostream>
6 #include <cmath>
7 #include <string>
8 using namespace std;
9
10 class CashSuper
11 {
12 public:
13 virtual double acceptMoney(double money) = 0;
14 };
15
16 class CashNormal : public CashSuper
17 {
18 public:
19 double acceptMoney(double money)
20 {
21 return money;
22 }
23 };
24
25 class CashRebate : public CashSuper
26 {
27 private:
28 double discount;
29
30 public:
31 CashRebate(double dis)
32 {
33 discount = dis;
34 }
35 double acceptMoney(double money)
36 {
37 return money * discount;
38 }
39 };
40
41
42 class CashReturn : public CashSuper
43 {
44 private:
45 double moneyCondition;
46 double moneyReturn;
47
48 public:
49 CashReturn(double mc, double mr)
50 {
51 moneyCondition = mc;
52 moneyReturn = mr;
53 }
54 double acceptMoney(double money)
55 {
56 double result = money;
57 if (money >= moneyCondition)
58 {
59 result = money - floor(money / moneyCondition) * moneyReturn;
60 }
61 return result;
62 }
63 };
64
65 class CashContext
66 {
67 private:
68 CashSuper *cs;
69 public:
70 CashContext(CashSuper *cs)
71 {
72 this->cs = cs;
73 }
74 double getResult(double money)
75 {
76 return cs->acceptMoney(money);
77 }
78 };
79
80 void main()
81 {
82 CashSuper *cs;
83 CashContext *cc;
84 double money = 1000;
85
86 cs = new CashRebate(0.8);
87 cc = new CashContext(cs);
88 cout << cc->getResult(money) << endl;
89
90 money = 1000;
91 cs = new CashNormal();
92 cc = new CashContext(cs);
93 cout << cc->getResult(money) << endl;
94
95 }
【3】策略与工厂结合模式代码示例
代码示例:
1 /*
2 * 策略与工厂模式
3 */
4 #include <iostream>
5 #include <cmath>
6 #include <string>
7 using namespace std;
8
9 class CashSuper
10 {
11 public:
12 virtual double acceptMoney(double money) = 0;
13 };
14
15 class CashNormal : public CashSuper
16 {
17 public:
18 double acceptMoney(double money)
19 {
20 return money;
21 }
22 };
23
24 class CashRebate : public CashSuper
25 {
26 private:
27 double discount;
28 public:
29 CashRebate(double dis)
30 {
31 discount = dis;
32 }
33 double acceptMoney(double money)
34 {
35 return money * discount;
36 }
37 };
38
39
40 class CashReturn : public CashSuper
41 {
42 private:
43 double moneyCondition;
44 double moneyReturn;
45 public:
46 CashReturn(double mc, double mr)
47 {
48 moneyCondition = mc;
49 moneyReturn = mr;
50 }
51 double acceptMoney(double money)
52 {
53 double result = money;
54 if (money >= moneyCondition)
55 {
56 result = money - floor(money / moneyCondition) * moneyReturn;
57 }
58 return result;
59 }
60 };
61
62 class CashContext
63 {
64 private:
65 CashSuper *cs;
66 public:
67 CashContext(string str)
68 {
69 if ("正常收费" == str)
70 {
71 cs = new CashNormal();
72 }
73 else if ("打9折" == str)
74 {
75 cs = new CashRebate(0.9);
76 }
77 else if ("满300送200" == str)
78 {
79 cs = new CashReturn(300, 200);
80 }
81 }
82 double getResult(double money)
83 {
84 return cs->acceptMoney(money);
85 }
86 };
87
88
89 int main()
90 {
91 double money = 1000;
92 CashContext *cc = new CashContext("打9折");
93 cout << cc->getResult(money);
94 return 0;
95 }
Good Good Study, Day Day Up.
顺序 选择 循环 总结