中介者模式
中介者模式是一种行为设计模式, 能让你减少对象之间混乱无序的依赖关系。 该模式会限制对象之间的直接交互, 迫使它们通过一个中介者对象进行合作。
中介者模式(Mediator Pattern ),用一个中介对象来封装一系列的对象交互。中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
中介者模式又叫调停模式,它是迪米特法则的典型应用。
类似比如MVC模式,C(Controller控制器)是M(Model模型)和V(View视图)的中介者,在前后端交互时起到了中间人的作用。
概括:定义一个中介对象来简化原有对象之间的交互关系,降低系统中对象间的耦合度,使原有对象之间不必相互了解。
模式结构
- 抽象中介者(Mediator)角色:它是中介者的接口,提供了同事对象注册与转发同事对象信息的抽象方法。
- 具体中介者(Concrete Mediator)角色:实现中介者接口,定义一个 List 来管理同事对象,协调各个同事角色之间的交互关系,因此它依赖于同事角色。
- 抽象同事类(Colleague)角色:定义同事类的接口,保存中介者对象,提供同事对象交互的抽象方法,实现所有相互影响的同事类的公共功能。
- 具体同事类(Concrete Colleague)角色:是抽象同事类的实现者,当需要与其他同事对象交互时,由中介者对象负责后续的交互。
类图
优缺点
优点:
- 单一职责原则。 你可以将多个组件间的交流抽取到同一位置, 使其更易于理解和维护。
- 开闭原则。 你无需修改实际组件就能增加新的中介者。
- 减少类间依赖,降低了耦合,符合迪米特原则
- 多个类相互耦合,会形成网状结构, 使用中介者模式将网状结构分离为星型结构,进行解耦
- 将对象间的一对多关联转变为一对一的关联,提高系统的灵活性,使得系统易于维护和扩展
缺点:
- 当同事类越多时,中介者类设计不合理就会变得复杂且难以维护。
- 中介者承担了较多的责任,一旦中介者出现了问题,整个系统就会受到影响
- 如果设计不当,中介者对象本身变得过于复杂 。
应用场景
-
当对象之间存在复杂的网状结构关系而导致依赖关系混乱且难以复用时。
-
如果为了能在不同情景下复用一些基本行为, 导致你需要被迫创建大量组件子类时, 可使用中介者模式。
-
当想创建一个运行于多个类之间的对象,又不想生成新的子类时
示例代码
public interface Colleague {
void send(String sendName);
void receive(String receiveName);
void setMeditor(Meditor meditor);
}
public class ConcreteColleagueA implements Colleague{
Meditor meditor;
@Override
public void send(String sendName) {
System.out.println("向中介者发出请求:和"+sendName+"沟通");
meditor.relay(this,sendName);
}
@Override
public void receive(String receiverName) {
System.out.println("收到中介者转发来的消息:"+receiverName+"需要和我沟通交流");
}
@Override
public void setMeditor(Meditor meditor) {
this.meditor = meditor;
}
}
public class ConcreteColleagueB implements Colleague{
Meditor meditor;
public void send( String sendName) {
System.out.println("向中介者发出请求:和"+sendName+"沟通");
meditor.relay(this,sendName);
}
@Override
public void receive(String receiverName) {
System.out.println("收到中介者发来的消息:"+receiverName+"需要和我沟通");
}
@Override
public void setMeditor(Meditor meditor) {
this.meditor = meditor;
}
}
public class ConcreteMeditor implements Meditor{
private final ArrayList< Colleague> colleagues;
public ConcreteMeditor() {
colleagues = new ArrayList<>();
}
@Override
public void register(Colleague colleague) {
if (!colleagues.contains(colleague)){
colleagues.add(colleague);
colleague.setMeditor(this);
}
}
@Override
public void remove(Colleague colleague) {
colleagues.remove(colleague);
}
@Override
public void relay(Colleague colleague,String relayName) {
colleague.receive(relayName);
}
}
public interface Meditor {
void register(Colleague colleague);
void remove(Colleague colleague);
void relay(Colleague colleague,String relayName);
}
public class MeditorPattern {
public static void main(String[] args) {
Colleague concreteColleagueA = new ConcreteColleagueA();
Colleague concreteColleagueB = new ConcreteColleagueB();
Meditor meditor = new ConcreteMeditor();
meditor.register(concreteColleagueA);
meditor.register(concreteColleagueB);
concreteColleagueA.send("B");
System.out.println("***********");
concreteColleagueB.send("A");
}
}