中介者模式(Mediator)

中介者模式是一种行为设计模式,可以减少对象之间混乱无序的依赖关系。该模式会限制对象之间的直接交互,迫使它们通过一个封装了对象间交互行为的中介者对象来进行合作,从而使对象间耦合松散,并可独立地改变它们之间的交互。中介者模式又称为调停者模式。

Mediator is a behavior design pattern. It can reduce the disordered dependencies between objects. 
This pattern restricts direct interaction between objects, forcing them to collaborate through a mediator object 
that encapsulates the interaction behavior between objects, resulting in loose coupling between objects 
and the ability to independently change their interactions.  

结构设计

中介者模式包含如下角色:
Component,组件基类,声明组件的基本功能,有一个指向中介者的引用,该引用被声明为中介者接口类型。组件不知道中介者实际所属的类,因此可通过将其连接到不同的中介者以使其能在其他程序中复用。
Mediator,中介者接口,声明了与组件交流的方法,但通常仅包括一个通知方法。组件可将任意上下文作为该方法的参数,只有这样接收组件和发送者类之间才不会耦合。
ConcreteComponent,具体组件,实现组件声明的方法,并自定义业务逻辑接口。
ConcreteMediator,具体中介者,实现中介者接口声明的方法。
中介者模式类图表示如下:
请添加图片描述

伪代码实现

接下来将使用代码介绍下中介者模式的实现。

// 1、中介者接口,声明了与组件交流的方法  
public interface IMediator {void notify(Sender sender);
}//2、具体中介者,实现中介者接口声明的方法
public class ConcreteMediator implements IMediator {@Overridepublic void notify(Sender sender) {String message = sender.getMessage();Component target = sender.getTarget();target.operation(message);}
}// 3、组件基类,声明组件的基本功能,有一个指向中介者的引用,该引用被声明为中介者接口类型
public abstract class Component {protected IMediator mediator;public Component(IMediator mediator) {this.mediator = mediator;}public void operation(String message) {System.out.println("message is " + message);}public void send(String message, Component target) {Sender sender = new Sender(message, this, target);mediator.notify(sender);}
}// 4、具体组件,实现组件声明的方法,并自定义业务逻辑接口
public class ConcreteComponentA extends Component {public ConcreteComponentA(IMediator mediator) {super(mediator);}@Overridepublic void operation(String message) {super.operation(message);operationA();}public void operationA() {System.out.println("operationA in a Concrete ComponentA instance");}
}
public class ConcreteComponentB extends Component {public ConcreteComponentB(IMediator mediator) {super(mediator);}@Overridepublic void operation(String message) {super.operation(message);operationB();}public void operationB() {System.out.println("operationB in a Concrete ComponentB instance");}
}
public class ConcreteComponentC extends Component {public ConcreteComponentC(IMediator mediator) {super(mediator);}@Overridepublic void operation(String message) {super.operation(message);operationC();}public void operationC() {System.out.println("operationC in a Concrete ComponentC instance");}
}
public class ConcreteComponentD extends Component {public ConcreteComponentD(IMediator mediator) {super(mediator);}@Overridepublic void operation(String message) {super.operation(message);operationD();}public void operationD() {System.out.println("operationD in a Concrete ComponentD instance");}
}// 5、客户端
public class MediatorClient {public void test() {IMediator mediator = new ConcreteMediator();Component componentA = new ConcreteComponentA(mediator);Component componentB = new ConcreteComponentB(mediator);Component componentC = new ConcreteComponentC(mediator);Component componentD = new ConcreteComponentD(mediator);componentA.send("i am a", componentB);componentB.send("i am b", componentC);componentC.send("i am c", componentD);componentD.send("i am d", componentA);}
}public class Sender {private String message;private Component source;private Component target;public Sender(String message, Component source, Component target) {this.message = message;this.source = source;this.target = target;}public String getMessage() {return this.message;}public Component getSource() {return this.source;}public Component getTarget() {return this.target;}
}

适用场景

在以下情况下可以考虑使用中介者模式:
(1) 当一些对象和其他对象紧密耦合,产生的相互依赖关系结构混乱且难以理解,从而导致难以对其进行修改时,可考虑使用中介者模式。中介者模式可将对象间的所有关系抽取成为一个单独的类,
以使对于特定组件的修改工作独立于其他组件。
(2) 当组件因过于依赖其他组件而无法在不同应用中复用时,可考虑使用中介者模式。应用中介者模式后, 每个组件不再知晓其他组件的情况。尽管这些组件无法直接交流,但它们仍可通过中介者对象进行间接交流。
如果希望在不同应用中复用一个组件,则需要为其提供一个新的中介者类。
(3) 如果为了能在不同情景下复用一些基本行为,导致需要被迫创建大量组件子类时,可考虑使用中介者模式。由于所有组件间关系都被包含在中介者中, 因此无需修改组件就能方便地新建中介者类以定义新的组件合作方式。

优缺点

中介者模式有以下优点:
(1) 符合单一职责原则。可以将多个组件间的交流抽取到同一位置,使其更易于理解和维护。
(2) 符合开闭原则。无需修改实际组件就能增加新的中介者。
(3) 可以减轻应用中多个组件间的耦合情况。
但是该模式也存在以下缺点:
(1) 在具体中介者类中包含了组件之间的交互细节,可能会导致具体中介者类非常复杂,使得系统难以维护。一段时间后,中介者可能会演化成为上帝对象。

参考

《设计模式 可复用面向对象软件的基础》 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 著, 李英军, 马晓星等译
https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/mediator.html 中介者模式
https://refactoringguru.cn/design-patterns/mediator 中介者模式
https://www.runoob.com/design-pattern/mediator-pattern.html 中介者模式
https://www.cnblogs.com/adamjwh/p/10959987.html 简说设计模式——中介者模式

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/53824.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【项目 线程4】3.12生产者消费者模型 3.13条件变量 3.14信号量 C++实现生产者消费者模型

3.12生产者消费者模型 生产者消费者模型中的对象: 1、生产者 2、消费者 3、容器 若容器已满,生产者阻塞在这,通知消费者去消费;若容器已空,则消费者阻塞,通知生产者去生产。生产者可以有多个,消…

深入学习 Redis - 谈谈你对 Redis 的 RDB、AOF、混合持久化的了解吧?

目录 一、Redis 是怎么存储数据的? 二、Redis 具体是按照什么样的策略来实现持久化的? 2.1、RDB(Redis Database) 2.1.1、触发机制 2.1.2、bgsave 命令处理流程 2.1.3、RDB 文件的处理 2.1.4、演示效果 1)手动执…

OSPF在MGRE上的实验

实验题目如下: 实验拓扑如下: 实验要求如下: 【1】R6为ISP只能配置ip地址,R1-5的环回为私有网段 【2】R1/4/5为全连的MGRE结构,R1/2/3为星型的拓扑结构,R1为中心站点 【3】所有私有网段可以互相通讯&…

【ARM Coresight 系列文章 2.3 - Coresight 寄存器】

文章目录 Coresight 寄存器介绍1.1 ITCTRL,integration mode control register1.2 CLAIM寄存器1.3 DEVAFF(Device Affinity Registers)1.4 LSR and LAR1.5 AUTHSTATUS(Authentication Status Register) Coresight 寄存器介绍 Coresight 对于每个 coresight 组件&am…

Android中级——RemoteView

RemoteView RemoteView的应用NotificationWidgetPendingIntent RemoteViews内部机制模拟RemoteViews RemoteView的应用 Notification 如下开启一个系统的通知栏,点击后跳转到某网页 public class MainActivity extends AppCompatActivity {private static final …

屏幕取色器Mac版_苹果屏幕取色工具_屏幕取色器工具

Sip for Mac 是Mac系统平台上的一款老牌的颜色拾取工具,是设计师和前端开发工作者必不可少的屏幕取色软件,你只需要用鼠标点一下即可轻松地对屏幕上的任何颜色进行采样和编码,并将颜色数据自动存到剪切板,方便随时粘贴出来。 Sip…

Aligning Large Language Models with Human: A Survey

本文也是LLM相关的综述文章,针对《Aligning Large Language Models with Human: A Survey》的翻译。 对齐人类与大语言模型:综述 摘要1 引言2 对齐数据收集2.1 来自人类的指令2.1.1 NLP基准2.1.2 人工构造指令 2.2 来自强大LLM的指令2.2.1 自指令2.2.2 …

# 关于Linux下的parted分区工具显示起始点为1049kB的问题解释

关于Linux下的parted分区工具显示起始点为1049kB的问题解释 文章目录 关于Linux下的parted分区工具显示起始点为1049kB的问题解释1 问题展示:2 原因3 修改为KiB方式显示4 最后 1 问题展示: kevinTM1701-b38cbc23:~$ sudo parted /dev/nvme1n1 GNU Part…

分治法、回溯法与动态规划

算法思想比较 回溯法:有“通用解题法”之称,用它可以系统地搜索问题的所有解。回溯法是按照深度优先搜索(DFS)的策略,从根结点出发深度探索解空间树分治法:将一个难以直接解决的大问题,分割成一些规模较小的相同问题&…

测试平台——项目模块模型类设计

这里写目录标题 一、项目应用1、项目包含接口:2、创建子应用3、项目模块设计a、模型类设计b、序列化器类设计c、视图类设计 4、接口模块设计a、模型类设计b、序列化器类设计c、视图类设计 5、环境模块设计6、DRF中的通用过滤6.1、设置过滤器后端 一、项目应用 1、项…

程序环境和预处理(含C语言程序的编译+链接)--1

🎉个人名片: 🐼作者简介:一名乐于分享在学习道路上收获的大二在校生 🐻‍❄个人主页🎉:GOTXX 🐼个人WeChat:ILXOXVJE 🐼本文由GOTXX原创,首发CSDN…

Palo Alto Networks® PA-220R 下一代防火墙 确保恶劣工况下的网络安全

一、主要安全功能 1、每时每刻在各端口对全部应用进行分类 • 将 App-ID 用于工业协议和应用,例如 Modbus、 DNP3、IEC 60870-5-104、Siemens S7、OSIsoft PI 等。 • 不论采用何种端口、SSL/SSH 加密或者其他规避技术,都会识别应用。 • 使用…