一、定义
中介者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显作用,从而使它们可以松散耦合。
二、使用场景
当对象之间的交互操作很多且每个对象的行为操作都依赖彼此时,为防止在修改一个对象的行为时,同时涉及修改很多其他对象的行为,可采用中介者模式,来解决紧耦合问题。
三、UML类图
Mediator:抽象中介者,定义了同事对象到中介者对象的接口。
ConcreteMediator:具体中介者,实现抽象中介者定义的方法,从具体同事对象接收消息,向具体同事对象发出命令。
Colleague:抽象同事类,定义了中介者对象的接口,它只知道中介者而不知道其他的同事对象。
ConcreteColleague:具体同事类,每个具体同事类都知道本身在小范围内的行为,而不知道它在大范围内的目的。
四、实现
//抽象中介者
public abstract class Mediator {
protected ConcreteColleagueA colleagueA;
protected ConcreteColleagueB colleagueB;
public abstract void method();
public void setColleagueA(ConcreteColleagueA colleagueA) {
this.colleagueA = colleagueA;
}
public void setColleagueB(ConcreteColleagueB colleagueB) {
this.colleagueB = colleagueB;
}
}
//具体中介者
public class ConcreteMediator extends Mediator {
@Override
public void method() {
colleagueA.action();
colleagueB.action();
}
}
//抽象同事
public abstract class Colleague {
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
public abstract void action();
}
//具体同事A
public class ConcreteColleagueA extends Colleague {
public ConcreteColleagueA(Mediator mediator) {
super(mediator);
}
@Override
public void action() {
System.out.println("ColleagueA将信息交给中介者处理");
}
}
//具体同事B
public class ConcreteColleagueB extends Colleague {
public ConcreteColleagueB(Mediator mediator) {
super(mediator);
}
@Override
public void action() {
System.out.println("ColleagueB将信息交给中介者处理");
}
}
五、应用
例子:电脑主机
//抽象中介者
public abstract class Mediator {
public abstract void changed(Colleague c);
}
//主板中介者
public class MainBoard extends Mediator {
private CDDevice cdDevice;
private CPU cpu;
private SoundCard soundCard;
private GraphicsCard graphicsCard;
@Override
public void changed(Colleague c) {
if(c == cdDevice) { //如果是光驱读取了数据
handleCD((CDDevice)c);
} else if(c == cpu) { //如果CPU处理完数据
handleCPU((CPU)c)
}
}
private void handleCD(CDDevice cdDevice) {
cpu.decodeData(cdDevice.read());
}
private void handleCPU(CPU cpu) {
soundCard.soundPlay(cpu.getDataSound);
graphicsCard.videoPlay(cpu.getDataVideo());
}
public void setCDDevice(CDDevice cdDevice) {
this.cdDevice = cdDevice;
}
public void setCPU(CPU cpu) {
this.cpu = cpu;
}
public void setSoundCard(SoundCard soundCard) {
this.soundCard = soundCard;
}
public void setGraphicsCard(GraphicsCard graphicsCard) {
this.graphicsCard = graphicsCard;
}
}
//抽象同事
public abstract class Colleague {
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
}
//CPU同事
public class CPU extends Colleague {
private String dataVideo, dataSound;
public CPU(Mediator mediator) {
super(mediator);
}
public String getDataVideo() {
return dataVideo;
}
public String getDataSound() {
return dataSound;
}
public void decodeData(String data) {
String[] tmp = data.split(",");
dataVideo = tmp[0];
dataSound = tmp[1];
mediator.changed(this);
}
}
//光驱同事
public class CDDevice extends Colleague {
private String data;
public CDDevice(Mediator mediator) {
super(mediator);
}
public String read() {
return data;
}
public void load() {
data = "视频数据,音频数据";
mediator.changed(this);
}
}
//显卡同事
public class GraphicsCard extends Colleague {
public GraphicsCard(Mediator mediator) {
super(mediator);
}
public void videoPlay(String data) {
System.out.println("视频:" + data);
}
}
//声卡同事
public class SoundCard extends Colleague {
public SoundCard(Mediator mediator) {
super(mediator);
}
public void soundPlay(String data) {
System.out.println("音频:" + data);
}
}
//客户端
public class Client {
public static void main(String[] args) {
MainBoard mediator = new MainBoard();
CDDevice cd = new CDDevice(mediator);
CPU cpu = new CPU(mediator);
GraphicsCard vc = new GraphicsCard(mediator);
SoundCard sc = new SoundCard(mediator);
mediator.setCDDevice(cd);
mediator.setCPU(cpu);
mediator.setGraphicsCard(vc);
mediator.setSoundCard(sc);
cd.load();
}
}
六、总结
中介者模式将对象之间的多对多关系变成一对多关系,中介者对象将系统从网状结构变成星形结构,达到降低系统的复杂性,提高可扩展性的作用。