-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStrategy_II.h
More file actions
118 lines (106 loc) · 3.36 KB
/
Copy pathStrategy_II.h
File metadata and controls
118 lines (106 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// 策略模式与简单工厂结合 —— 商场促销计算器
//
#ifndef DESIGNPATTERN_STRATEGY_II_H
#define DESIGNPATTERN_STRATEGY_II_H
// 基本的收银类(基类)
class CashierS {
public:
virtual double AcceptCash(double money) {
double change = 0.0;
return change;
}
};
// 正常收银(派生类)
class CommonCashierS: public CashierS {
public:
double AcceptCash(double money) override {
return money;
}
};
// 商场打折(派生类)
class DiscountCashierS: public CashierS {
private:
double _discount = 0.0;
public:
explicit DiscountCashierS(double discount) {
if(discount <= 0 || discount >= 10) {
throw "Invalid discount settings!";
}
_discount = discount / 10.0;
}
double AcceptCash(double money) override {
return money * _discount;
}
};
// 商场满减(派生类)
class RefundCashierS: public CashierS {
private:
int _condition = 0;
int _refund = 0;
public:
RefundCashierS(int condition, int refund) {
_condition = condition;
_refund = refund;
}
double AcceptCash(double money) override {
if(money < _condition) {
return money;
} else {
int t = static_cast<int>(money) / _condition;
return money - t * _refund;
}
}
};
// 策略模式与工厂模式结合
class CashierContextS {
private:
double _discount = 0.0;
int _condition = 0;
int _refund = 0;
CashierS* strategy = nullptr;
public:
void SetDiscount(double discount) {
_discount = discount;
}
void SetRefundCondition(int condition, int refund) {
_condition = condition;
_refund = refund;
}
explicit CashierContextS(char mode, double discount, int condition, int refund) {
switch(mode) {
case 'c':
strategy = new CommonCashierS();
break;
case 'd':
SetDiscount(discount);
strategy = new DiscountCashierS(_discount);
break;
case 'r':
SetRefundCondition(condition, refund);
strategy = new RefundCashierS(_condition, _refund);
break;
default:
break;
}
}
double GetResult(double money) {
return strategy->AcceptCash(money);
}
};
// 改进策略模式 —— 客户端
class MallS {
public:
double GetCashier(double money, double discount, int condition, int refund, char mode) {
auto csr = new CashierContextS(mode, discount, condition, refund);
return csr->GetResult(money);
}
};
/********************************************************************************************
* // 策略模式客户端 *
* MallS m; *
* cout << "收银1000元,无优惠,合计:" << m.GetCashier(1000, 0, 0, 0, 'c') << endl; *
* cout << "收银1000元,打8折,合计:" << m.GetCashier(1000, 8, 0, 0, 'd') << endl; *
* cout << "收银1000元,满200减50,合计:" << m.GetCashier(1000, 0, 200, 50, 'r') << endl; *
********************************************************************************************/
#endif //DESIGNPATTERN_STRATEGY_II_H