diff --git a/README.md b/README.md index 552ec2a..e6e2135 100644 --- a/README.md +++ b/README.md @@ -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 | ✔ | diff --git a/structural/facade/main.go b/structural/facade/main.go new file mode 100644 index 0000000..953e6fd --- /dev/null +++ b/structural/facade/main.go @@ -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() +} + + + +