awesome-patterns/structural/facade/main.go

77 lines
1.4 KiB
Go
Raw Normal View History

2019-05-05 12:18:49 +03:00
// 外观模式 facade pattern.
// 为多个子模块提供一个统一的调用接口,子模块可以是同一接口的实现也可以不同.
2019-05-05 18:42:21 +03:00
// 实际上编写程序的时候很多地方不知不觉的会使用该模式.
// 这里以购买鸡蛋,牛奶,小麦粉为例,从代购处一次性购买三种食材而不需要分别访问三个商店,
// SellAll()方法还可以将三处接口包装为原子操作,在购买失败时进行回滚
2019-05-05 12:18:49 +03:00
package main
2019-05-05 18:42:21 +03:00
import (
"errors"
"fmt"
)
2019-05-05 12:18:49 +03:00
type Shop interface {
2019-05-05 18:42:21 +03:00
Sell() error
2019-05-05 12:18:49 +03:00
}
type EggShop struct {}
2019-05-05 18:42:21 +03:00
func (EggShop)Sell() error{
return errors.New("no more eggs left")
2019-05-05 12:18:49 +03:00
}
type MilkShop struct {}
2019-05-05 18:42:21 +03:00
func (MilkShop)Sell()error{
return errors.New("no more milk left")
2019-05-05 12:18:49 +03:00
}
type WheatFlourShop struct {}
2019-05-05 18:42:21 +03:00
func (WheatFlourShop)Sell()error{
return errors.New("no more wheat flour left")
2019-05-05 12:18:49 +03:00
}
type DealerFacade struct {
EgShop Shop
MkShop Shop
WfShop Shop
}
2019-05-05 18:42:21 +03:00
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)
}
2019-05-05 12:18:49 +03:00
}
func main(){
dealer := DealerFacade{
EggShop{},
MilkShop{},
WheatFlourShop{},
}
2019-05-05 18:42:21 +03:00
dealer.BuyAll()
/*
output:
error:
no more eggs left
no more milk left
no more wheat flour left
*/
2019-05-05 12:18:49 +03:00
}