中介者设计模式
- 🚩什么是中介者设计模式?
- 🚩中介者设计模式的特点
- 🚩中介者设计模式的结构
- 🚩中介者设计模式的优缺点
- 🚩中介者设计模式的Java实现
- 🚩代码总结
- 🚩总结
🚩什么是中介者设计模式?
中介者设计模式(Mediator Pattern) 是一种 行为型设计模式,它通过定义一个中介对象来封装一系列对象之间的交互,使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。中介者模式又称为调停者模式。
使用场景
-
当系统中 对象之间存在复杂的网状引用关系,导致系统结构混乱时
-
当需要 通过一个中间类来封装多个类之间的交互 时
-
当 不希望对象直接相互引用,而是通过中介者进行通信时
-
适用于 聊天系统、GUI组件交互、MVC框架 等场景
🚩中介者设计模式的特点
-
减少对象间直接依赖:对象间不直接通信,而是通过中介者
-
集中控制交互:所有交互逻辑集中在中介者中
-
简化对象设计:各个对象只需与中介者交互
-
降低耦合度:对象间不再相互持有引用
-
易于扩展:新增同事类只需修改中介者
🚩中介者设计模式的结构
中介者模式主要包含以下部分:
-
Mediator(抽象中介者):定义同事对象到中介者对象的接口
-
ConcreteMediator(具体中介者):实现抽象中介者的接口,协调各同事对象
-
Colleague(抽象同事类):定义同事类的接口,持有中介者引用
-
ConcreteColleague(具体同事类):实现抽象同事类,与其他同事通过中介者通信
图例:
🚩中介者设计模式的优缺点
✅ 优点
-
降低耦合度:将网状结构变为星型结构,减少对象间依赖
-
简化对象协议:用一对多的交互代替多对多的交互
-
集中控制交互:将交互行为集中在一个中介者对象中
-
易于扩展:新增同事类只需修改中介者类
-
符合迪米特法则:减少对象间的直接通信
❌ 缺点
-
中介者可能变得复杂:随着同事类增多,中介者会变得庞大复杂
-
过度集中化:中介者承担过多职责可能成为"上帝对象"
-
性能问题:所有通信都经过中介者可能影响性能
🚩中介者设计模式的Java实现
代码地址:GitHub
- 创建
Mediator(抽象中介者)
接口
/*** @author hanson.huang* @version V1.0* @InterfaceName Mediator* @Description 抽象中介者* @date 2025/3/25 19:38**/
public interface Mediator {void sendMessage(String message, Colleague colleague);
}
- 创建
Colleague(抽象同事类)
抽象类
/*** @author hanson.huang* @version V1.0* @ClassName Colleague* @Description 抽象同事类* @date 2025/3/25 19:38**/
public abstract class Colleague {protected Mediator mediator;
}
-
创建具体同事类
Colleague1
/*** @author hanson.huang* @version V1.0* @ClassName Colleague1* @Description 具体同事类1* @date 2025/3/25 19:40**/ public class Colleague1 extends Colleague {public Colleague1(Mediator mediator) {this.mediator = mediator;}public void sendMessage(String message) {mediator.sendMessage(message, this);}public void Notify(String message) {System.out.println("同事1收到消息:" + message);} }
Colleague2
/*** @author hanson.huang* @version V1.0* @ClassName Colleague2* @Description 具体同事类2* @date 2025/3/25 19:41**/ public class Colleague2 extends Colleague {public Colleague2(Mediator mediator) {this.mediator = mediator;}public void sendMessage(String message) {mediator.sendMessage(message, this);}public void Notify(String message) {System.out.println("同事2收到消息:" + message);} }
-
创建
ConcreteMediator(具体中介者)
/*** @author hanson.huang* @version V1.0* @ClassName ConcreteMediator* @Description 具体中介者* @date 2025/3/25 19:41**/
public class ConcreteMediator implements Mediator{private Colleague1 colleague1;private Colleague2 colleague2;public void setColleague1(Colleague1 colleague1) {this.colleague1 = colleague1;}public void setColleague2(Colleague2 colleague2) {this.colleague2 = colleague2;}@Overridepublic void sendMessage(String message, Colleague colleague) {if (colleague == colleague1) {colleague2.Notify(message);} else {colleague1.Notify(message);}}
}
- 测试中介者模式
/*** @author hanson.huang* @version V1.0* @ClassName MediatorPattern* @Description 测试中介者模式* @date 2025/3/25 19:44**/
public class MediatorPattern {public static void main(String[] args) {ConcreteMediator mediator = new ConcreteMediator();Colleague1 colleague1 = new Colleague1(mediator);Colleague2 colleague2 = new Colleague2(mediator);mediator.setColleague1(colleague1);mediator.setColleague2(colleague2);colleague1.sendMessage("软考加油");colleague2.sendMessage("祝大家软考顺利通过!");}
}
📌 运行结果
🚩代码总结
-
Mediator
接口定义中介者的通信协议 -
Colleague
抽象类是所有同事类的基类,持有中介者引用 -
Colleague1
和Colleague2
是具体同事类,通过中介者通信 -
ConcreteMediator
是具体中介者,协调同事对象间的交互 -
MediatorPattern
客户端创建中介者和同事对象并设置它们的关系
🚩总结
-
中介者设计模式 通过引入中介者对象来封装对象间的交互
-
核心是 将网状的对象引用关系变为星型结构,降低对象间耦合
-
适用于 对象间交互复杂且相互依赖 的场景
✅ Java源码中的应用场景:
-
Java消息服务(JMS):
- 消息队列作为中介者协调消息生产者和消费者
-
MVC框架:
- 控制器(
Controller
)作为模型(Model
)和视图(View
)的中介者
- 控制器(
创作不易,不妨点赞、收藏、关注支持一下,各位的支持就是我创作的最大动力❤️