2 策略模式(2)

代码
1 #include <iostream>
2 #include <string>
3
4  using std::string;
5 using std::cout;
6 using std::endl;
7 using std::cin;
8
9 //现金收费抽象类
10 class CashSuper
11 {
12 public:
13 virtual double AcceptCash(double money) = 0;
14 };
15
16 //正常收费子类
17 class CashNormal : public CashSuper
18 {
19 public:
20 double AcceptCash(double money)
21 {
22 return money;
23 }
24 };
25
26 //打折收费子类
27 class CashRebate : public CashSuper
28 {
29 public:
30 CashRebate(double moneyRebate)
31 {
32 this->moneyRebate = moneyRebate;
33 }
34 double AcceptCash(double money)
35 {
36 return money * moneyRebate;
37 }
38 private:
39 double moneyRebate;
40 };
41
42 //返回收费子类
43 class CashReturn : public CashSuper
44 {
45 public:
46 CashReturn(double moneyCondition, double moneyReturn)
47 {
48 this->moneyCondition = moneyCondition;
49 this->moneyReturn = moneyReturn;
50 }
51 double AcceptCash(double money)
52 {
53 if(money > moneyCondition)
54 {
55 return money - (int) money / (int)moneyCondition * moneyReturn;
56 }
57 else
58 {
59 return money;
60 }
61 }
62 private:
63 double moneyCondition;
64 double moneyReturn;
65 };
66
67 //如果使用工厂模式
68 class CashFactory
69 {
70 public:
71 static CashSuper* CreateCashAccept(string type)
72 {
73 CashSuper* cs = NULL;
74 if(type == "正常收费")
75 {
76 cs = new CashNormal();
77 }
78 else if(type == "满300送100")
79 {
80 cs = new CashReturn(300, 100);
81 }
82 else if(type == "打8折")
83 {
84 cs = new CashRebate(0.8);
85 }
86 else
87 {
88 cs = new CashNormal();
89 }
90 return cs;
91
92 }
93 };
94
95 //如果使用策略模式
96 class CashContext
97 {
98 public:
99 //通过构造方法传入具体的收费策略
100 CashContext(CashSuper* csuper)
101 {
102 this->cs = csuper;
103 }
104 //策略模式与工厂模式结合
105 //参数不是具体的策略对象,而是一个字符串,表示收费类型
106 CashContext(string type)
107 {
108 if(type == "正常收费")
109 {
110 cs = new CashNormal();
111 }
112 else if(type == "满300送100")
113 {
114 cs = new CashReturn(300, 100);
115 }
116 else if(type == "打8折")
117 {
118 cs = new CashRebate(0.8);
119 }
120 else
121 {
122 cs = new CashNormal();
123 }
124 }
125 double GetResult(double money)
126 {
127 return cs->AcceptCash(money);
128 }
129 private:
130 CashSuper* cs;
131 };
132
133 int main()
134 {
135 //如果使用工厂模式
136 CashSuper* cs = CashFactory::CreateCashAccept("正常收费");
137 cout<<cs->AcceptCash(500)<<endl;
138
139 //如果使用策略模式
140 CashContext* cc = NULL;
141
142 string type;
143 cin>>type;
144 if(type == "正常收费")
145 {
146 cc = new CashContext(new CashNormal());
147 }
148 else if(type == "满300送100")
149 {
150 cc = new CashContext(new CashReturn(300, 100));
151 }
152 else if(type == "打8折")
153 {
154 cc = new CashContext(new CashRebate(0.8));
155 }
156
157 cout<<cc->GetResult(500)<<endl;
158
159 //如果使用策略模式和工厂模式结合
160 cc = new CashContext(type);
161 cout<<cc->GetResult()<<endl;
162
163 //“简单工厂模式我需要让客户端认识两个类,CashSuper和CashFactory,而策略模式与简单工厂模式结合的用法,客户端就只需要认识一个雷CashContext就可以了。耦合更加降低”
164 //“说的没错,我们在客户端实例化的是CashContext的对象,调用的是CashContext的方法GetResult,这使得具体的收费算法彻底地与客户端分离。连算法的父类CashSuper都不让客户端认识了。”
165
166 return 0;
167 }

 

posted @ 2010-04-24 23:38  斯芬克斯  阅读(228)  评论(0编辑  收藏  举报