forked from loveshes/go-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
60 lines (51 loc) · 1.12 KB
/
strategy.go
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
package strategy
//【策略模式】
// 3种具体的打折策略
const (
ORIGINAL_PRICE = iota // 0
TEN_PERCENT_OFF // 1
FULL_100_MINUS_20 // 2
)
// 打折接口
type Pricer interface {
Price(cash float64) float64
}
// 原价
type normal struct {
}
// 打折
type discount struct {
percent float64
}
// 每满fullMoney就减discountMoney
type reduction struct {
fullMoney float64
discountMoney float64
}
// 均实现了pricer接口
func (n *normal) Price(cash float64) float64 {
return cash
}
func (d *discount) Price(cash float64) float64 {
return cash * d.percent
}
func (r *reduction) Price(cash float64) float64 {
count := cash / r.fullMoney
return cash - count*r.discountMoney
}
// 生成具体策略的工厂
type CashFactory struct {
}
// 将具体的策略实现封装起来,交由工厂来实现
func (c CashFactory) GetCashPrice(discountType int) Pricer {
var cashPricer Pricer
switch discountType {
case ORIGINAL_PRICE:
cashPricer = &normal{}
case TEN_PERCENT_OFF:
cashPricer = &discount{0.9}
case FULL_100_MINUS_20:
cashPricer = &reduction{100, 20}
}
return cashPricer
}