电子商务网站开发的题,wordpress 菜单图标,陕西西安网络公司,wordpress淘宝客排名主题中介者模式是一种行为型设计模式#xff0c;它用于减少对象之间的直接依赖关系。通过引入一个中介者对象#xff0c;所有对象的交互都通过中介者进行#xff0c;而不是直接相互通信。这种模式的主要目的是减少对象之间的耦合#xff0c;提升系统的灵活性和可维护性。
1. 定…中介者模式是一种行为型设计模式它用于减少对象之间的直接依赖关系。通过引入一个中介者对象所有对象的交互都通过中介者进行而不是直接相互通信。这种模式的主要目的是减少对象之间的耦合提升系统的灵活性和可维护性。
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 Colleague25. 优点
减少类之间的依赖通过中介者模式同事类不再直接引用彼此而是通过中介者对象进行通信从而减少了类之间的耦合。集中化通信逻辑中介者将交互的逻辑集中在一个地方便于维护和扩展。扩展性强如果添加新的同事类只需要在中介者中进行适配而不需要修改现有的类。
6. 缺点
中介者的复杂性当系统中存在大量同事对象时中介者对象的职责会变得非常复杂可能会导致中介者本身的代码膨胀。过度集中如果所有的交互都通过中介者可能会导致中介者对象成为“上帝对象”承担过多的职责变得难以维护。
7. 应用场景
中介者模式适用于以下情况
复杂的对象交互当多个对象之间有复杂的交互关系时使用中介者模式能够简化这些交互避免直接依赖。解耦合如果对象之间需要进行频繁的交互但又不希望它们之间过于紧密地耦合可以通过中介者来解耦。多个对象的通信协调适用于需要协调多个对象行为的场景比如 UI 界面中多个控件之间的交互。
8. 总结
中介者模式通过将对象间的交互集中到一个中介者对象中降低了对象之间的耦合度简化了对象间的通信逻辑。它使得系统更容易维护和扩展但也可能导致中介者过于复杂成为系统的瓶颈。因此在使用中介者模式时需要权衡其优缺点根据具体的需求来选择是否使用。
版权声明
本文内容属于原创欢迎转载但请务必注明出处和作者尊重原创版权。转载时请附带原文链接并注明“本文作者扣丁梦想家禁止未经授权的商业转载。
如果您有任何问题或建议欢迎留言讨论。