设计模式22:Mediator Pattern (调解者模式)

本文详细介绍了调解者模式,一种用于降低对象间耦合的设计模式。通过实例代码展示了如何实现调解者模式,以及在实际场景中如何使用该模式进行通信,如在一个聊天室应用中实现参与者之间的松散耦合交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

英文原文:http://www.dofactory.com/Patterns/PatternMediator.aspx

一、Mediator Pattern (调解者模式)的定义

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

定义一个对象将一系列对象如何相互作用的操作进行封装。调解者通过避免一个对象明确指向其余的对象来促使自由耦合,同时能够让你单独的改变他们之间的相互作用。

二、UML类图

Median

  • Mediator  (IChatroom)
    • 定义一个接口用于与同事级对象进行通信
  • ConcreteMediator  (Chatroom)
    • 通过与同事级对象进行对等来实现相互协作行为
    • 知道并且维持本身的同事级对象
  • Colleague classes  (Participant)
    • 每个同事级对象都知道自己的调节者对象
    • 每个同事级对象在与另外的同事级对象进行通信时必须要与调节者进行通信。

    三、调解者模式实例代码

    四、另外一个使用调解者模式的实例代码

    This real-world code demonstrates the Mediator pattern facilitating loosely coupled communication between different Participants registering with a Chatroom. The Chatroom is the central hub through which all communication takes place. At this point only one-to-one communication is implemented in the Chatroom, but would be trivial to change to one-to-many.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值