go-pattern-examples/structure/01_facade/facade.go

86 lines
1.4 KiB
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package facade
2020-04-28 06:41:19 +03:00
import (
"fmt"
)
2020-04-21 17:50:21 +03:00
2020-04-28 06:41:19 +03:00
//ISupertMarketVendor is facade interface of facade package
type ISupertMarketVendor interface {
Sell(count int)
2020-04-21 17:50:21 +03:00
}
2020-04-28 06:41:19 +03:00
//SaltVendor 盐供应商
type SaltVendor struct{}
2020-04-21 17:50:21 +03:00
2020-04-28 06:41:19 +03:00
//MilkVendor 牛奶供应商
type MilkVendor struct{}
2020-04-21 17:50:21 +03:00
2020-04-28 06:41:19 +03:00
//RiceVendor 大米供应商
type RiceVendor struct{}
//三家供应商都能直接卖东西
//Sell 卖完了
func (SaltVendor) Sell(count int) {
if count > 5 {
fmt.Println("Salt out")
}
fmt.Println("Milk got")
2020-04-21 17:50:21 +03:00
}
2020-04-28 06:41:19 +03:00
//Sell 卖完了
func (MilkVendor) Sell(count int) {
if count > 20 {
fmt.Println("Milk out")
}
fmt.Println("Milk got")
2020-04-21 17:50:21 +03:00
}
2020-04-28 06:41:19 +03:00
//Sell 卖完了
func (RiceVendor) Sell(count int) {
2020-04-21 17:50:21 +03:00
2020-04-28 06:41:19 +03:00
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
2020-04-21 17:50:21 +03:00
}
2020-04-28 06:41:19 +03:00
//ISupertMarket market can do
type ISupertMarket interface {
Sell(salt, milk, rice int)
2020-04-21 17:50:21 +03:00
}
2020-04-28 06:41:19 +03:00
//Sell 集中购买
func (s *SuperMarket) Sell(salt, milk, rice int) {
s.saltsVendor.Sell(salt)
s.milksVendor.Sell(milk)
s.ricesVendor.Sell(rice)
2020-04-21 17:50:21 +03:00
}
2020-04-28 06:41:19 +03:00
//NewSuperMarket get a market
func NewSuperMarket() ISupertMarket {
2020-04-21 17:50:21 +03:00
2020-04-28 06:41:19 +03:00
return &SuperMarket{
saltsVendor: MilkVendor{},
milksVendor: MilkVendor{},
ricesVendor: RiceVendor{},
}
2020-04-21 17:50:21 +03:00
}