一、概念
定义一个中介对象来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。中介者模式又叫调停模式,它是迪米特法则的典型应用
二、组成角色
- 抽象中介者(Mediator)角色:它是中介者的接口,提供了同事对象注册与转发同事对象信息的抽象方法。
- 具体中介者(Concrete Mediator)角色:实现中介者接口,定义一个 List 来管理同事对象,协调各个同事角色之间的交互关系,因此它依赖于同事角色
- 抽象同事类(Colleague)角色:定义同事类的接口,保存中介者对象,提供同事对象交互的抽象方法,实现所有相互影响的同事类的公共功能
- 具体同事类(Concrete Colleague)角色:是抽象同事类的实现者,当需要与其他同事对象交互时,由中介者对象负责后续的交互
三、优缺点
优点
- 类之间各司其职,符合迪米特法则
- 降低了对象之间的耦合性
- 将对象间的一对多关联转变为一对一的关联,提高系统的灵活性,使得系统易于维护和扩展
- 中介者模式便与集中控制对象之间的信号交流
- 各个组件变得更简单并且更容易处理,因为它们不需要彼此传递消息
- 这些组件不需要包含逻辑来处理它们之间的相互通信,因此它们更加通用
缺点
- 中介者模式将原本多个对象直接的相互依赖变成了中介者和多个同事类的依赖关系。当同事类越多时,中介者就会越臃肿,变得复杂且难以维护
四、业务分析
当今世界设有联合国来处理国际间的事务,中美日三国之间的政治经济事务很多情况下需要借助联合国来处理;而国家通常在其他各国设有大使馆,并且有其他机构。联合国可以通知任一国家的所有机构,也可通知任一国家的任一机构
五、代码实现
UN
package com.behaviour.intermediary.Mediator.Mediator;
import com.behaviour.intermediary.Mediator.Colleague.Country;
/**
* 抽象中介者(Mediator)
* 联合国抽象接口
*/
public abstract class UN {
//注册同事类
public abstract void register(Country country);
//转发消息
public abstract void send(Class type, String msg);
public abstract void send(Object obj, String msg);
}
UnitedNations
package com.behaviour.intermediary.Mediator.ConcreteMediator;
import com.behaviour.intermediary.Mediator.Colleague.Country;
import com.behaviour.intermediary.Mediator.Mediator.UN;
import java.util.ArrayList;
import java.util.List;
/**
* 具体中介者(Concrete Mediator)
* 联合国
*/
public class UnitedNations extends UN {
private List<Country> countries;
public UnitedNations(){
this.countries = new ArrayList<>();
}
@Override
public void register(Country country) {
this.countries.add(country);
country.setUn(this);
}
@Override
public void send(Class type, String msg) {
for (Country temp : this.countries) {
if(type.equals(temp.getClass())){
temp.receive(msg);
}
}
}
@Override
public void send(Object country, String msg) {
for (Country temp : this.countries) {
if(temp == country){
temp.receive(msg);
}
}
}
}
Country
package com.behaviour.intermediary.Mediator.Colleague;
/**
* 抽象同事类(Colleague
* 国家
*/
import com.behaviour.intermediary.Mediator.Mediator.UN;
public abstract class Country {
protected UN un;
protected String name;
public Country(String name){
this.name = name;
}
public void setUn(UN un){
this.un = un;
}
public abstract void receive(String msg);
public abstract void send(Class country, String msg);
public abstract void send(Object depart, String msg);
}
China
package com.behaviour.intermediary.Mediator.ConcreteColleague;
import com.behaviour.intermediary.Mediator.Colleague.Country;
/**
* 具体同事类(Concrete Colleague)
* 中国
*/
public class China extends Country {
public China(String name) {
super("中国" + name);
}
@Override
public void receive(String msg) {
System.out.println(name + "收到了联合国的通知:" + msg);
}
@Override
public void send(Class country, String msg) {
System.out.println(name + "发表了声明:" + msg);
this.un.send(country, msg);
}
@Override
public void send(Object depart, String msg) {
System.out.println(name + "发表了声明:" + msg);
this.un.send(depart, msg);
}
}
USA
package com.behaviour.intermediary.Mediator.ConcreteColleague;
import com.behaviour.intermediary.Mediator.Colleague.Country;
/**
* 具体同事类(Concrete Colleague)
* 美国
*/
public class USA extends Country {
public USA(String name) {
super("美国" + name);
}
@Override
public void receive(String msg) {
System.out.println(name + "收到了联合国的通知:" + msg);
}
@Override
public void send(Class country, String msg) {
System.out.println(name + "发送了声明:" + msg);
this.un.send(country, msg);
}
@Override
public void send(Object depart, String msg) {
System.out.println(name + "发表了声明:" + msg);
this.un.send(depart, msg);
}
}
Japanese
package com.behaviour.intermediary.Mediator.ConcreteColleague;
import com.behaviour.intermediary.Mediator.Colleague.Country;
/**
* 具体同事类(Concrete Colleague)
* 日本
*/
public class Japanese extends Country {
public Japanese(String name) {
super("日本" + name);
}
@Override
public void receive(String msg) {
System.out.println(name + "收到了联合国的通知:" + msg);
}
@Override
public void send(Class country, String msg) {
System.out.println(name + "发表了声明:" + msg);
this.un.send(country, msg);
}
@Override
public void send(Object depart, String msg) {
System.out.println(name + "发表了声明:" + msg);
this.un.send(depart, msg);
}
}
Test
package com.behaviour.intermediary.Mediator;
import com.behaviour.intermediary.Mediator.Colleague.Country;
import com.behaviour.intermediary.Mediator.ConcreteColleague.China;
import com.behaviour.intermediary.Mediator.ConcreteColleague.Japanese;
import com.behaviour.intermediary.Mediator.ConcreteColleague.USA;
import com.behaviour.intermediary.Mediator.ConcreteMediator.UnitedNations;
import com.behaviour.intermediary.Mediator.Mediator.UN;
public class Test {
public static void main(String[] args) {
UN un = new UnitedNations();
Country china_one = new China("驻美大使馆");
Country china_two = new China("驻日本大使馆");
Country china_three = new China("驻澳大利亚大使馆");
Country usa = new USA("国防部");
Country japanese = new Japanese("外交部");
un.register(china_one);
un.register(china_two);
un.register(china_three);
un.register(usa);
un.register(japanese);
usa.send(China.class, "美国决定对中国取消关税!");
usa.send(Japanese.class, "日本停止挑衅中国,否则自取灭亡");
usa.send(china_one, "美国白宫请求中国不要撤走中国驻美大使馆");
}
}