中介者模式(Mediator Pattern)
1.策略模式(Strategy Pattern)2.桥接模式(Bridge Pattern)3.代理模式(Proxy Pattern)
4.中介者模式(Mediator Pattern)
5.过滤器模式(Filter Pattern)6.适配器模式(Adapter Pattern)7.责任链模式(Chain Of Responsibility Pattern)8.装饰器模式(Decorator Pattern)9.建造者模式(Builder Pattern)10.观察者模式(Observer Pattern)11.工厂模式(Factory Pattern)12.单例模式(Singleton Pattern)13.组合模式(Composite Pattern)14.模板模式(Template Pattern)15.原型模式(Prototype Pattern)16.外观模式(Facade Pattern)17.享元模式(Flyweight Pattern)18.命令模式(Command Pattern)19.解释器模式(Interpreter Pattern)20.迭代器模式(Iterator Pattern)中介者模式
一、概述
中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。中介者模式属于行为型模式。
二、介绍
用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
中介者模式将多个类相互耦合,形成的网状结构,分离为星型结构。
关键代码:对象 Colleague 之间的通信封装到一个类中单独处理。
它主要包含如下几个角色:
- Mediator: 抽象中介者。定义了同事对象到中介者对象之间的接口。
- ConcreteMediator: 具体中介者。实现抽象中介者的方法,它需要知道所有的具体同事类,同时需要从具体的同事类那里接收信息,并且向具体的同事类发送信息。
- Colleague: 抽象同事类。
- ConcreteColleague: 具体同事类。每个具体同事类都只需要知道自己的行为即可,但是他们都需要认识中介者。

优点:
- 降低了类的复杂度,将一对多转化成了一对一。
- 各个类之间的解耦。
- 符合迪米特原则(一个对象应当对其他对象有尽可能少的了解)。
缺点:中介者会庞大,变得复杂难以维护。
使用场景:
- 系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象。
- 想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。
三、代码实现
抽象中介类
package com.designpattern.mediatorPattern;
/**
* 抽象中介类
*
* @author zhongtao on 2018/12/19
*/
public abstract class Mediator {
public abstract void contact(String content, Colleague colleague);
}
抽象同事类
package com.designpattern.mediatorPattern;
/**
* 抽象同事类
*
* @author zhongtao on 2018/12/19
*/
public class Colleague {
protected String name;
protected Mediator mediator;
public Colleague(String name, Mediator mediator) {
this.name = name;
this.mediator = mediator;
}
}
具体同事类A和B
package com.designpattern.mediatorPattern;
/**
* 具体同事类A
*
* @author zhongtao on 2018/12/19
*/
public class ColleagueA extends Colleague {
public ColleagueA(String name, Mediator mediator) {
super(name, mediator);
}
public void getMessage(String message) {
System.out.println("同事A:" + name + " 获取信息:" + message);
}
public void contact(String message) {
mediator.contact(message, this);
}
}
/**
* 具体同事类B
*
* @author zhongtao on 2018/12/19
*/
public class ColleagueB extends Colleague {
public ColleagueB(String name, Mediator mediator) {
super(name, mediator);
}
public void getMessage(String message) {
System.out.println("同事B:" + name + " 获取信息:" + message);
}
public void contact(String message) {
mediator.contact(message, this);
}
}
具体中介类
package com.designpattern.mediatorPattern;
/**
* 具体中介类
*
* @author zhongtao on 2018/12/19
*/
public class ConcreteMediator extends Mediator {
private ColleagueA colleagueA;
private ColleagueB colleagueB;
public ColleagueA getColleagueA() {
return colleagueA;
}
public void setColleagueA(ColleagueA colleagueA) {
this.colleagueA = colleagueA;
}
public ColleagueB getColleagueB() {
return colleagueB;
}
public void setColleagueB(ColleagueB colleagueB) {
this.colleagueB = colleagueB;
}
@Override
public void contact(String content, Colleague colleague) {
if (colleague == colleagueA) {
colleagueB.getMessage(content);
} else {
colleagueA.getMessage(content);
}
}
}
客户端 测试中介者模式
package com.designpattern.mediatorPattern;
/**
* 客户端 测试中介者模式
*
* @author zhongtao on 2018/12/19
*/
public class Client {
public static void main(String[] args) {
ConcreteMediator mediator = new ConcreteMediator();
ColleagueA peter = new ColleagueA("Peter", mediator);
ColleagueB lina = new ColleagueB("Lina", mediator);
mediator.setColleagueA(peter);
mediator.setColleagueB(lina);
peter.contact("我是peter,想请lina晚上看电影");
lina.contact("我是lina,可以滴");
}
}
测试结果
同事B:Lina 获取信息:我是peter,想请lina晚上看电影
同事A:Peter 获取信息:我是lina,可以滴
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类