mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-25 21:26:03 +03:00
finish facade pattern
This commit is contained in:
parent
c4c60eea07
commit
9bc6dc571d
@ -2,4 +2,8 @@
|
||||
|
||||
又称门面模式,APIs Gateway就是一种facade模式的,用户只要通过APIs就可访问系统功能.
|
||||
|
||||
这个模式正如New模式一样,已经熟悉到了被经常忽略的地步了,常用在用集中访问的场景,比如政务部门集中办事窗口,开学集中报道注册等.
|
||||
|
||||
Service Desk 或者Calling Center 呼叫中心也是典型门面模式场景,对一些问题的处理和访问,都通过一个集中的入口统一访问.
|
||||
|
||||
日常的超市/商店就是一个很典型门面模式,日常用品你不用去各个进货商那里买想要的东西,只需要去超市能集中买到你想要的所有东西.
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
//API is facade interface of facade package
|
||||
type API interface {
|
||||
Test() string
|
||||
//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")
|
||||
|
||||
}
|
||||
|
||||
//facade implement
|
||||
type apiImpl struct {
|
||||
a AModuleAPI
|
||||
b BModuleAPI
|
||||
//Sell 卖完了
|
||||
func (MilkVendor) Sell(count int) {
|
||||
|
||||
if count > 20 {
|
||||
fmt.Println("Milk out")
|
||||
|
||||
}
|
||||
fmt.Println("Milk got")
|
||||
|
||||
}
|
||||
|
||||
func (a *apiImpl) Test() string {
|
||||
aRet := a.a.TestA()
|
||||
bRet := a.b.TestB()
|
||||
return fmt.Sprintf("%s\n%s", aRet, bRet)
|
||||
//Sell 卖完了
|
||||
func (RiceVendor) Sell(count int) {
|
||||
|
||||
if count > 10 {
|
||||
fmt.Println("Rice out")
|
||||
|
||||
}
|
||||
fmt.Println("Rice got")
|
||||
|
||||
}
|
||||
|
||||
//NewAModuleAPI return new AModuleAPI
|
||||
func NewAModuleAPI() AModuleAPI {
|
||||
return &aModuleImpl{}
|
||||
//SuperMarket is facade implement
|
||||
//SuperMarket is Facade object
|
||||
//SuperMarket 具有集中进货能力
|
||||
type SuperMarket struct {
|
||||
saltsVendor ISupertMarketVendor
|
||||
milksVendor ISupertMarketVendor
|
||||
ricesVendor ISupertMarketVendor
|
||||
}
|
||||
|
||||
//AModuleAPI ...
|
||||
type AModuleAPI interface {
|
||||
TestA() string
|
||||
//ISupertMarket market can do
|
||||
type ISupertMarket interface {
|
||||
Sell(salt, milk, rice int)
|
||||
}
|
||||
|
||||
type aModuleImpl struct{}
|
||||
|
||||
func (*aModuleImpl) TestA() string {
|
||||
return "A module running"
|
||||
//Sell 集中购买
|
||||
func (s *SuperMarket) Sell(salt, milk, rice int) {
|
||||
s.saltsVendor.Sell(salt)
|
||||
s.milksVendor.Sell(milk)
|
||||
s.ricesVendor.Sell(rice)
|
||||
}
|
||||
|
||||
//NewBModuleAPI return new BModuleAPI
|
||||
func NewBModuleAPI() BModuleAPI {
|
||||
return &bModuleImpl{}
|
||||
}
|
||||
//NewSuperMarket get a market
|
||||
func NewSuperMarket() ISupertMarket {
|
||||
|
||||
//BModuleAPI ...
|
||||
type BModuleAPI interface {
|
||||
TestB() string
|
||||
return &SuperMarket{
|
||||
saltsVendor: MilkVendor{},
|
||||
milksVendor: MilkVendor{},
|
||||
ricesVendor: RiceVendor{},
|
||||
}
|
||||
|
||||
type bModuleImpl struct{}
|
||||
|
||||
func (*bModuleImpl) TestB() string {
|
||||
return "B module running"
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user