-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.go
76 lines (61 loc) · 1.39 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
// 外观模式 facade pattern.
// 为多个子模块提供一个统一的调用接口,子模块可以是同一接口的实现也可以不同.
// 实际上编写程序的时候很多地方不知不觉的会使用该模式.
// 这里以购买鸡蛋,牛奶,小麦粉为例,从代购处一次性购买三种食材而不需要分别访问三个商店,
// SellAll()方法还可以将三处接口包装为原子操作,在购买失败时进行回滚
package main
import (
"errors"
"fmt"
)
type Shop interface {
Sell() error
}
type EggShop struct {}
func (EggShop)Sell() error{
return errors.New("no more eggs left")
}
type MilkShop struct {}
func (MilkShop)Sell()error{
return errors.New("no more milk left")
}
type WheatFlourShop struct {}
func (WheatFlourShop)Sell()error{
return errors.New("no more wheat flour left")
}
type DealerFacade struct {
EgShop Shop
MkShop Shop
WfShop Shop
}
func (d DealerFacade)BuyAll(){
//if e := d.EgShop.Sell();e != nil{
// log.Println(e)
// RollBack()
//}
//...
e1 := d.EgShop.Sell()
e2 := d.MkShop.Sell()
e3 := d.WfShop.Sell()
if e1 == nil && e2 == nil && e3 == nil{
//success
}else{
//fail and rollback
fmt.Printf("error:\n%v\n%v\n%v" ,e1 ,e2 ,e3)
}
}
func main(){
dealer := DealerFacade{
EggShop{},
MilkShop{},
WheatFlourShop{},
}
dealer.BuyAll()
/*
output:
error:
no more eggs left
no more milk left
no more wheat flour left
*/
}