[go] 中介者模式

设计模式

中介者模式是一种行为设计模式, 能让你减少对象之间混乱无序的依赖关系。 该模式会限制对象之间的直接交互, 迫使它们通过一个中介者对象进行合作。

模型说明

  1. 组件(Component)是各种包含业务逻辑的类。每个组件都有一个指向中介者的引用,该引用被声明为中介者接口类型。组件不知道中介者实际所属的类,因此你可通过将其连接到不同的中介者以使其能在其他程序中复用。

  2. 中介者(Mediator)接口声明了与组件交流的方法,但通常仅包括一个通知方法。组件可将任意上下文(包括自己的对象)作为该方法的参数,只有这样接收组件和发送者类之间才不会耦合。

  3. 具体中介者(Concrete Mediator)封装了多种组件间的关系。具体中介者通常会保存所有组件的引用并对其进行管理,甚至有时会对其生命周期进行管理。

  4. 组件并不知道其他组件的情况。如果组件内发生了重要事件,它只能通知中介者。中介者收到通知后能轻易地确定发送者,这或许已足以判断接下来需要触发的组件了。

  • 对于组件来说,中介者看上去完全就是一个黑箱。发送者不知道最终会由谁来处理自己的请求,接收者也不知道最初是谁发出了请求。

优缺点

1.优点

  • 单一职责原则:你可以将多个组件间的交流抽取到同一位置,使其更易于理解和维护。
  • 开闭原则:你无需修改实际组件就能增加新的中介者。
  • 你可以减轻应用中多个组件间的耦合情况。
  • 你可以更方便地复用各个组件。

2.缺点

  • 一段时间后,中介者可能会演化成为上帝对象

使用场景

  • 当一些对象和其他对象紧密耦合以致难以对其进行修改时,可使用中介者模式。
  • 当组件因过于依赖其他组件而无法在不同应用中复用时,可使用中介者模式。
  • 如果为了能在不同情景下复用一些基本行为,导致你需要被迫创建大量组件子类时,可使用中介者模式。

参考代码

火车站交通系统。 两列火车互相之间从来不会就站台的空闲状态进行通信。
train.go: 组件

go 复制代码
package main

type Train interface {
    arrive()
    depart()
    permitArrival()
}

passengerTrain.go: 具体组件

go 复制代码
package main

import "fmt"

type PassengerTrain struct {
    mediator Mediator
}

func (g *PassengerTrain) arrive() {
    if !g.mediator.canArrive(g) {
        fmt.Println("PassengerTrain: Arrival blocked, waiting")
        return
    }
    fmt.Println("PassengerTrain: Arrived")
}

func (g *PassengerTrain) depart() {
    fmt.Println("PassengerTrain: Leaving")
    g.mediator.notifyAboutDeparture()
}

func (g *PassengerTrain) permitArrival() {
    fmt.Println("PassengerTrain: Arrival permitted, arriving")
    g.arrive()
}

freightTrain.go: 具体组件

go 复制代码
package main

import "fmt"

type FreightTrain struct {
    mediator Mediator
}

func (g *FreightTrain) arrive() {
    if !g.mediator.canArrive(g) {
        fmt.Println("FreightTrain: Arrival blocked, waiting")
        return
    }
    fmt.Println("FreightTrain: Arrived")
}

func (g *FreightTrain) depart() {
    fmt.Println("FreightTrain: Leaving")
    g.mediator.notifyAboutDeparture()
}

func (g *FreightTrain) permitArrival() {
    fmt.Println("FreightTrain: Arrival permitted")
    g.arrive()
}

mediator.go: 中介者接口

go 复制代码
package main

type Mediator interface {
    canArrive(Train) bool
    notifyAboutDeparture()
}

stationManager.go: 具体中介者

go 复制代码
package main

type StationManager struct {
    isPlatformFree bool
    trainQueue     []Train
}

func newStationManger() *StationManager {
    return &StationManager{
        isPlatformFree: true,
    }
}

func (s *StationManager) canArrive(t Train) bool {
    if s.isPlatformFree {
        s.isPlatformFree = false
        return true
    }
    s.trainQueue = append(s.trainQueue, t)
    return false
}

func (s *StationManager) notifyAboutDeparture() {
    if !s.isPlatformFree {
        s.isPlatformFree = true
    }
    if len(s.trainQueue) > 0 {
        firstTrainInQueue := s.trainQueue[0]
        s.trainQueue = s.trainQueue[1:]
        firstTrainInQueue.permitArrival()
    }
}

main.go: 客户端代码

go 复制代码
package main

func main() {
    stationManager := newStationManger()

    passengerTrain := &PassengerTrain{
        mediator: stationManager,
    }
    freightTrain := &FreightTrain{
        mediator: stationManager,
    }

    passengerTrain.arrive()
    freightTrain.arrive()
    passengerTrain.depart()
}

output:

go 复制代码
PassengerTrain: Arrived
FreightTrain: Arrival blocked, waiting
PassengerTrain: Leaving
FreightTrain: Arrival permitted
FreightTrain: Arrived
相关推荐
niceffking2 分钟前
C++:initializer_list 与 {} 初始化的本质
开发语言·c++·cpp
jaysee-sjc3 分钟前
十六、Java 网络编程全解析:UDP/TCP 通信 + BS/CS 架构
java·开发语言·网络·tcp/ip·算法·架构·udp
江沉晚呤时5 分钟前
基于 AssemblyLoadContext 的 .NET 插件化架构设计与实现
开发语言·c#·.net
2501_9307077810 分钟前
使用C#代码获取PDF文件的页数
开发语言·pdf·c#
.select.13 分钟前
虚函数和虚表
开发语言·c++·算法
王ASC13 分钟前
Java不重启加载新的class文件
java·开发语言
乐观勇敢坚强的老彭16 分钟前
c++信奥for循环强化03
开发语言·c++
咚为17 分钟前
告别 lazy_static:深度解析 Rust OnceCell 的前世今生与实战
开发语言·后端·rust
全栈开发圈24 分钟前
干货分享|R语言聚类分析1
开发语言·r语言
Aawy12027 分钟前
C++与Rust交互编程
开发语言·c++·算法