finish facade pattern

This commit is contained in:
Edward 2020-04-28 11:41:19 +08:00
parent c4c60eea07
commit 9bc6dc571d
3 changed files with 91 additions and 62 deletions

View File

@ -2,4 +2,8 @@
又称门面模式APIs Gateway就是一种facade模式的用户只要通过APIs就可访问系统功能. 又称门面模式APIs Gateway就是一种facade模式的用户只要通过APIs就可访问系统功能.
这个模式正如New模式一样已经熟悉到了被经常忽略的地步了,常用在用集中访问的场景,比如政务部门集中办事窗口,开学集中报道注册等.
Service Desk 或者Calling Center 呼叫中心也是典型门面模式场景,对一些问题的处理和访问,都通过一个集中的入口统一访问. Service Desk 或者Calling Center 呼叫中心也是典型门面模式场景,对一些问题的处理和访问,都通过一个集中的入口统一访问.
日常的超市/商店就是一个很典型门面模式,日常用品你不用去各个进货商那里买想要的东西,只需要去超市能集中买到你想要的所有东西.

View File

@ -1,59 +1,85 @@
package facade package facade
import "fmt" import (
"fmt"
)
func NewAPI() API { //ISupertMarketVendor is facade interface of facade package
return &apiImpl{ type ISupertMarketVendor interface {
a: NewAModuleAPI(), Sell(count int)
b: NewBModuleAPI(),
}
} }
//API is facade interface of facade package //SaltVendor 盐供应商
type API interface { type SaltVendor struct{}
Test() string
//MilkVendor 牛奶供应商
type MilkVendor struct{}
//RiceVendor 大米供应商
type RiceVendor struct{}
//三家供应商都能直接卖东西
//Sell 卖完了
func (SaltVendor) Sell(count int) {
if count > 5 {
fmt.Println("Salt out")
}
fmt.Println("Milk got")
} }
//facade implement //Sell 卖完了
type apiImpl struct { func (MilkVendor) Sell(count int) {
a AModuleAPI
b BModuleAPI if count > 20 {
fmt.Println("Milk out")
}
fmt.Println("Milk got")
} }
func (a *apiImpl) Test() string { //Sell 卖完了
aRet := a.a.TestA() func (RiceVendor) Sell(count int) {
bRet := a.b.TestB()
return fmt.Sprintf("%s\n%s", aRet, bRet) if count > 10 {
fmt.Println("Rice out")
}
fmt.Println("Rice got")
} }
//NewAModuleAPI return new AModuleAPI //SuperMarket is facade implement
func NewAModuleAPI() AModuleAPI { //SuperMarket is Facade object
return &aModuleImpl{} //SuperMarket 具有集中进货能力
type SuperMarket struct {
saltsVendor ISupertMarketVendor
milksVendor ISupertMarketVendor
ricesVendor ISupertMarketVendor
} }
//AModuleAPI ... //ISupertMarket market can do
type AModuleAPI interface { type ISupertMarket interface {
TestA() string Sell(salt, milk, rice int)
} }
type aModuleImpl struct{} //Sell 集中购买
func (s *SuperMarket) Sell(salt, milk, rice int) {
func (*aModuleImpl) TestA() string { s.saltsVendor.Sell(salt)
return "A module running" s.milksVendor.Sell(milk)
s.ricesVendor.Sell(rice)
} }
//NewBModuleAPI return new BModuleAPI //NewSuperMarket get a market
func NewBModuleAPI() BModuleAPI { func NewSuperMarket() ISupertMarket {
return &bModuleImpl{}
}
//BModuleAPI ... return &SuperMarket{
type BModuleAPI interface { saltsVendor: MilkVendor{},
TestB() string milksVendor: MilkVendor{},
ricesVendor: RiceVendor{},
} }
type bModuleImpl struct{}
func (*bModuleImpl) TestB() string {
return "B module running"
} }

View File

@ -2,13 +2,12 @@ package facade
import "testing" import "testing"
var expect = "A module running\nB module running" func TestFacadeSuperMarket(t *testing.T) {
// TestFacadeAPI ... supermarket := NewSuperMarket()
func TestFacadeAPI(t *testing.T) {
api := NewAPI() supermarket.Sell(1, 3, 5)
ret := api.Test() supermarket.Sell(2, 11, 30)
if ret != expect {
t.Fatalf("expect %s, return %s", expect, ret) supermarket.Sell(8, 8, 30)
}
} }