facade pattern

This commit is contained in:
nynicg 2019-05-05 17:18:49 +08:00
parent 98163f1179
commit 280a0ab36a
2 changed files with 53 additions and 1 deletions

View File

@ -28,7 +28,7 @@ A curated collection of idiomatic design & application patterns for Go language.
| [Bridge](/structural/bridge/main.go) | Decouples an interface from its implementation so that the two can vary independently | ✔ |
| [Composite](/structural/composite/main.go) | Encapsulates and provides access to a number of different objects | ✔ |
| [Decorator](/structural/decorator.md) | Adds behavior to an object, statically or dynamically | ✔ |
| [Facade](/structural/facade.md) | Uses one type as an API to a number of others | ✘ |
| [Facade](/structural/facade/main.go) | Uses one type as an API to a number of others | ✔ |
| [Flyweight](/structural/flyweight.md) | Reuses existing instances of objects with similar/identical state to minimize resource usage | ✘ |
| [Proxy](/structural/proxy.md) | Provides a surrogate for an object to control it's actions | ✔ |

52
structural/facade/main.go Normal file
View File

@ -0,0 +1,52 @@
// 外观模式 facade pattern.
// 为多个子模块提供一个统一的调用接口,子模块可以是同一接口的实现也可以不同.
// 实际上编写程序的时候很多地方不知不觉的会使用了模式.
// 这里以购买鸡蛋,牛奶,小麦粉为例,从代购处一次性购买三种而不需要分别访问三个商店
package main
type Shop interface {
Sell()
}
type EggShop struct {}
func (EggShop)Sell(){
println("no more eggs left")
}
type MilkShop struct {}
func (MilkShop)Sell(){
println("no more milk left")
}
type WheatFlourShop struct {}
func (WheatFlourShop)Sell(){
println("no more wheat flour left")
}
type DealerFacade struct {
EgShop Shop
MkShop Shop
WfShop Shop
}
func (d DealerFacade)SellAll(){
d.EgShop.Sell()
d.MkShop.Sell()
d.WfShop.Sell()
}
func main(){
dealer := DealerFacade{
EggShop{},
MilkShop{},
WheatFlourShop{},
}
dealer.SellAll()
}