1. 类图
- Mediator:中介者接口。定义各个同事之间交互需要的方法
- ConcreteMediator:中介者实现。了解和维护各个同事,并负责协调同事间的交互关系
- Colleague:同事类父类,通常为抽象类。主要负责约束同事对象的类型,并实现一些具体同事类之间的公共功能
- ConcreteColleague:具体的同事类,需要与其他同事通信时,就持有中介者通信
2. 示例
将多个对象之间的交互统一封装
2.1 中介者接口
public interface Mediator {
void changed(Colleague colleague);
}
2.2 中介者实现类
public class MotherBoard implements Mediator {
// 持有所有同事对象
private CDDriver cdDriver = null;
private CPU cpu = null;
private VideoCard videoCard = null;
private SoundCard soundCard = null;
public void setCdDriver(CDDriver cdDriver) {this.cdDriver = cdDriver;}
public void setCpu(CPU cpu) {this.cpu = cpu;}
public void setVideoCard(VideoCard videoCard) {this.videoCard = videoCard;}
public void setSoundCard(SoundCard soundCard) {this.soundCard = soundCard;}
@Override
public void changed(Colleague colleague) {
if (colleague == cdDriver) {
this.operDriver((CDDriver)colleague);
} else if (colleague == cpu){
this.operCPU((CPU)cpu);
}
}
private void operDriver(CDDriver cdDriver){
// 1. 获取光驱读取到的数据
String data = cdDriver.getData();
// 2. 数据传给CPU进行处理
this.cpu.handleData(data);
}
private void operCPU(CPU cpu){
// 1. 获取CPU处理后的数据
String soundData = cpu.getSoundData();
String videoData = cpu.getVideoData();
// 2. 数据传递为声卡和显卡
this.videoCard.showData(videoData);
this.soundCard.soundData(soundData);
}
}
2.3 同事类父类
public abstract class Colleague {
private Mediator mediator;
public Colleague(Mediator mediator){this.mediator = mediator;}
public Mediator getMediator(){return this.mediator;}
}
2.4 同事类实现
public class CDDriver extends Colleague {
public CDDriver(Mediator mediator) {super(mediator);}
private String data = "";
public String getData(){return this.data;}
public void readCD(){
// 逗号前为视频数据,逗号后为声音数据
this.data = "中介者模式,学好了涨工资";
// 通知主板,光驱完成数据加载
this.getMediator().changed(this);
}
}
public class CPU extends Colleague {
public CPU(Mediator mediator) {super(mediator);}
// 视频数据
private String videoData = "";
// 声音数据
private String soundData = "";
public String getSoundData() {return soundData;}
public String getVideoData() {return videoData;}
public void handleData(String data){
String[] split = data.split(",");
this.videoData = split[0];
this.soundData = split[1];
// 通知主板,CPU完成数据处理
this.getMediator().changed(this);
}
}
public class VideoCard extends Colleague {
public VideoCard(Mediator mediator) {super(mediator); }
public void showData(String data){
System.out.println("hiloj正在看电脑,屏幕上显示内容为:" + data);
}
}
public class SoundCard extends Colleague {
public SoundCard(Mediator mediator) {super(mediator); }
public void soundData(String data){
System.out.println("hiloj正在看电脑,电脑音响里播放的声音为:" + data);
}
}
2.5 客户端
public class Client {
public static void main(String[] args) {
// 1. 创建主板对象
MotherBoard motherBoard = new MotherBoard();
// 2. 创建同事类
CDDriver cdDriver = new CDDriver(motherBoard);
CPU cpu = new CPU(motherBoard);
VideoCard videoCard = new VideoCard(motherBoard);
SoundCard soundCard = new SoundCard(motherBoard);
// 3. 让中介者知道所有的同事类
motherBoard.setCdDriver(cdDriver);
motherBoard.setCpu(cpu);
motherBoard.setVideoCard(videoCard);
motherBoard.setSoundCard(soundCard);
// 4. 放入光驱,光驱读盘
cdDriver.readCD();
}
}
3. 优点
- 松散耦合
- 集中控制交互
- 多对多变成一对多
4. 缺点
- 过度集中化,如果交互非常多,中介者对象会变得复杂且难以维护