GoLang设计模式10 - 中介者模式
2021/10/16 23:09:52
本文主要是介绍GoLang设计模式10 - 中介者模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
中介者模式是一种行为型设计模式。在中介者模式中创建了一个中介对象来负责不同类间的通信。因为这些类不需要直接交互,所以也就能避免它们之间的直接依赖,实现解耦的效果。
中介者模式的一个典型案例是老式小火车站。为保证铁路系统稳定运行,两列火车一般不会直接通信,而是听从车站管理员的调度。这里车站管理员就是一个中介者。他维护着一个火车进站的调度表,让火车站一次只允许一列火车停靠。当一列火车离开车站时,他才会通知调度表中的下一趟火车进站。
下面的代码模拟了小火车站管理员调度火车的工作。
火车接口 train.go :
type train interface { requestArrival() departure() permitArrival() }
客车类(passengerTrain)和货车类(goodsTrain)分别实现了train
接口。
passengerTrain.go :
import "fmt" type passengerTrain struct { mediator mediator } func (g *passengerTrain) requestArrival() { if g.mediator.canLand(g) { fmt.Println("PassengerTrain: Landing") } else { fmt.Println("PassengerTrain: Waiting") } } func (g *passengerTrain) departure() { fmt.Println("PassengerTrain: Leaving") g.mediator.notifyFree() } func (g *passengerTrain) permitArrival() { fmt.Println("PassengerTrain: Arrival Permitted. Landing") }
goodsTrain.go :
import "fmt" type goodsTrain struct { mediator mediator } func (g *goodsTrain) requestArrival() { if g.mediator.canLand(g) { fmt.Println("GoodsTrain: Landing") } else { fmt.Println("GoodsTrain: Waiting") } } func (g *goodsTrain) departure() { g.mediator.notifyFree() fmt.Println("GoodsTrain: Leaving") } func (g *goodsTrain) permitArrival() { fmt.Println("GoodsTrain: Arrival Permitted. Landing") }
中介者接口 mediator.go:
type mediator interface { canLand(train) bool notifyFree() }
车站管理员实现了mediator
接口, stationManager.go:
import "sync" type stationManager struct { isPlatformFree bool lock *sync.Mutex trainQueue []train } func newStationManger() *stationManager { return &stationManager{ isPlatformFree: true, lock: &sync.Mutex{}, } } func (s *stationManager) canLand(t train) bool { s.lock.Lock() defer s.lock.Unlock() if s.isPlatformFree { s.isPlatformFree = false return true } s.trainQueue = append(s.trainQueue, t) return false } func (s *stationManager) notifyFree() { s.lock.Lock() defer s.lock.Unlock() if !s.isPlatformFree { s.isPlatformFree = true } if len(s.trainQueue) > 0 { firstTrainInQueue := s.trainQueue[0] s.trainQueue = s.trainQueue[1:] firstTrainInQueue.permitArrival() } }
main.go:
func main() { stationManager := newStationManger() passengerTrain := &passengerTrain{ mediator: stationManager, } goodsTrain := &goodsTrain{ mediator: stationManager, } passengerTrain.requestArrival() goodsTrain.requestArrival() passengerTrain.departure() }
执行结果:
PassengerTrain: Landing GoodsTrain: Waiting PassengerTrain: Leaving GoodsTrain: Arrival Permitted. Landing
代码已上传至GitHub: zhyea / go-patterns / mediator-pattern
END!
这篇关于GoLang设计模式10 - 中介者模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-20go-zero 框架的 RPC 服务 启动start和停止 底层是怎么实现的?-icode9专业技术文章分享
- 2024-12-19Go-Zero 框架的 RPC 服务启动和停止的基本机制和过程是怎么实现的?-icode9专业技术文章分享
- 2024-12-18怎么在golang中使用gRPC测试mock数据?-icode9专业技术文章分享
- 2024-12-15掌握PageRank算法核心!你离Google优化高手只差一步!
- 2024-12-15GORM 中的标签 gorm:"index"是什么?-icode9专业技术文章分享
- 2024-12-11怎么在 Go 语言中获取 Open vSwitch (OVS) 的桥接信息(Bridge)?-icode9专业技术文章分享
- 2024-12-11怎么用Go 语言的库来与 Open vSwitch 进行交互?-icode9专业技术文章分享
- 2024-12-11怎么在 go-zero 项目中发送阿里云短信?-icode9专业技术文章分享
- 2024-12-11怎么使用阿里云 Go SDK (alibaba-cloud-sdk-go) 发送短信?-icode9专业技术文章分享
- 2024-12-10搭建个人博客网站之一、使用hugo创建个人博客网站