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

View File

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

View File

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