中介者模式是一种行为型设计模式,它用于减少对象之间的直接依赖关系。通过引入一个中介者对象,所有对象的交互都通过中介者进行,而不是直接相互通信。这种模式的主要目的是减少对象之间的耦合,提升系统的灵活性和可维护性。
1. 定义和意图
意图: 中介者模式通过一个中介者对象来协调和控制多个对象之间的交互,从而减少多个对象之间的直接联系,降低系统的复杂度。对象通过中介者进行通信,而不直接依赖于彼此。
2. 组成部分
中介者模式通常由以下几个角色组成:
- Mediator(中介者接口):声明了一个方法,用于同事对象之间的通信。
- ConcreteMediator(具体中介者):实现中介者接口,负责协调同事对象之间的交互。
- Colleague(同事类):定义同事对象,所有对象与中介者的交互都通过中介者进行。
- ConcreteColleague(具体同事类):继承自同事类,能够通过中介者与其他对象进行交互。
3. 结构图
+---------------------+| Mediator |+---------------------+| + notify() |+---------------------+^|+---------------------------+| ConcreteMediator |+---------------------------+| + colleague1: Colleague1 || + colleague2: Colleague2 |+---------------------------+|+--------------------------+------------------+| |
+---------------+ +------------------+
| Colleague1 | | Colleague2 |
+---------------+ +------------------+
| + setMediator()| | + setMediator() |
| + action() | | + action() |
+---------------+ +------------------+
4. 代码实现
4.1 中介者接口
首先,定义中介者接口,声明同事对象交互的方法。
// 中介者接口
public interface Mediator {void notify(Colleague colleague, String message);
}
4.2 具体中介者
然后,定义一个具体的中介者类,负责协调各个同事对象之间的交互。
// 具体中介者
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 notify(Colleague colleague, String message) {if (colleague == colleague1) {// 如果是colleague1,通知colleague2colleague2.receive(message);} else {// 如果是colleague2,通知colleague1colleague1.receive(message);}}
}
4.3 同事类
接下来,定义一个同事类的接口,所有同事类都必须实现这个接口。
// 同事类接口
public abstract class Colleague {protected Mediator mediator;public Colleague(Mediator mediator) {this.mediator = mediator;}// 设置中介者public void setMediator(Mediator mediator) {this.mediator = mediator;}// 接收消息public abstract void receive(String message);// 发送消息public abstract void send(String message);
}
4.4 具体同事类
然后,定义具体的同事类,负责执行具体的操作。
// 具体同事类1
public class Colleague1 extends Colleague {public Colleague1(Mediator mediator) {super(mediator);}@Overridepublic void receive(String message) {System.out.println("Colleague1 received: " + message);}@Overridepublic void send(String message) {System.out.println("Colleague1 sends: " + message);mediator.notify(this, message);}
}// 具体同事类2
public class Colleague2 extends Colleague {public Colleague2(Mediator mediator) {super(mediator);}@Overridepublic void receive(String message) {System.out.println("Colleague2 received: " + message);}@Overridepublic void send(String message) {System.out.println("Colleague2 sends: " + message);mediator.notify(this, message);}
}
4.5 使用示例
最后,创建一个应用程序来使用中介者模式。
public class Main {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.send("Hello from Colleague1!");colleague2.send("Hello from Colleague2!");}
}
4.6 输出结果
Colleague1 sends: Hello from Colleague1!
Colleague2 received: Hello from Colleague1
Colleague2 sends: Hello from Colleague2!
Colleague1 received: Hello from Colleague2
5. 优点
- 减少类之间的依赖:通过中介者模式,同事类不再直接引用彼此,而是通过中介者对象进行通信,从而减少了类之间的耦合。
- 集中化通信逻辑:中介者将交互的逻辑集中在一个地方,便于维护和扩展。
- 扩展性强:如果添加新的同事类,只需要在中介者中进行适配,而不需要修改现有的类。
6. 缺点
- 中介者的复杂性:当系统中存在大量同事对象时,中介者对象的职责会变得非常复杂,可能会导致中介者本身的代码膨胀。
- 过度集中:如果所有的交互都通过中介者,可能会导致中介者对象成为“上帝对象”,承担过多的职责,变得难以维护。
7. 应用场景
中介者模式适用于以下情况:
- 复杂的对象交互:当多个对象之间有复杂的交互关系时,使用中介者模式能够简化这些交互,避免直接依赖。
- 解耦合:如果对象之间需要进行频繁的交互,但又不希望它们之间过于紧密地耦合,可以通过中介者来解耦。
- 多个对象的通信协调:适用于需要协调多个对象行为的场景,比如 UI 界面中多个控件之间的交互。
8. 总结
中介者模式通过将对象间的交互集中到一个中介者对象中,降低了对象之间的耦合度,简化了对象间的通信逻辑。它使得系统更容易维护和扩展,但也可能导致中介者过于复杂,成为系统的瓶颈。因此,在使用中介者模式时,需要权衡其优缺点,根据具体的需求来选择是否使用。
版权声明
- 本文内容属于原创,欢迎转载,但请务必注明出处和作者,尊重原创版权。
- 转载时,请附带原文链接并注明“本文作者:扣丁梦想家
- 禁止未经授权的商业转载。
如果您有任何问题或建议,欢迎留言讨论。