-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstrategy.go
54 lines (43 loc) · 1.26 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
package strategy
import "fmt"
//money kind
const (
RMB = "RMB"
HK = "HK"
)
//StoreContext for Store 要包存钱的上下文信息
type StoreContext struct {
Kind, CardID string
Money int
}
//IStore 要实现的存钱接口
type IStore interface {
Store(*StoreContext)
}
//MainLandCitizen 大陆居民
type MainLandCitizen struct{ Name string }
//Store Money to bank
func (m *MainLandCitizen) Store(ctx *StoreContext) {
fmt.Println("i am: ", m.Name, "i want to store: ", ctx.Money, ctx.Kind, "to: ", ctx.CardID)
}
//HongKongCitizen 香港居民
type HongKongCitizen struct{ Name string }
//Store Money to bank
func (h *HongKongCitizen) Store(ctx *StoreContext) {
fmt.Println("i am: ", h.Name, "i want to store: ", ctx.Money, ctx.Kind, "to: ", ctx.CardID)
}
//Bank handle moneyholder
type Bank struct {
moneyHolder IStore
}
//Recept a user
func (b *Bank) Recept(moneyHolder IStore) {
b.moneyHolder = moneyHolder
fmt.Println("Bank: ", "Recept a New User")
}
//AccountUserMoney 动态替换的过程在这里,这里调用任何实现了Store的接口对象
//AccountUserMoney to handle User's Money
func (b *Bank) AccountUserMoney(ctx *StoreContext) {
b.moneyHolder.Store(ctx)
fmt.Println("Bank: ", "Processing Store", ctx.Money, ctx.Kind, "to: ", ctx.CardID)
}