原文地址 http://blog.csdn.net/qq_25806863/article/details/69396448
中介者模式又叫调解者模式或调停者模式,是行为型设计模式之一。
生活中的中介者的作用就是连接两方的一个桥梁,比如房产中介,买房的只需跟中介打交道,然后买房的也跟着中介打交道,
每个买房的和卖房的都要和很多对方打交道,有了新的买房人,这些卖房的都得知道才能去和他联系。
所有买房的和卖房的都只需要跟中介者一个人打交道,买房的不需要知道卖房的是什么人,有多少卖房的等等。都省事了很多。
通过中介者包装一系列对象的交互,使得这些对象不必相互显式引用,从而使它们可以松散耦合。
当某些对象之间的作用发生变化是,不会立即影响其他对象间的作用,保证这些作用协议彼此独立的变化。
中介者模式将多对多的相互作用转化为一堆多的相互作用。
模板代码:
抽象的中介者:
public interface Mediator {
void change();
}
具体的中介者:
public class ConcreteMediator implements Mediator {
public ConcreteColleagueA concreteColleagueA;
public ConcreteColleagueB concreteColleagueB;
public void setConcreteColleagueA(ConcreteColleagueA concreteColleagueA) {
this.concreteColleagueA = concreteColleagueA;
}
public void setConcreteColleagueB(ConcreteColleagueB concreteColleagueB) {
this.concreteColleagueB = concreteColleagueB;
}
@Override
public void change() {
concreteColleagueA.action();
concreteColleagueB.action();
}
}
抽象的同事:
public abstract class Colleague {
public Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
public abstract void action();
}
具体的同事:
public class ConcreteColleagueA extends Colleague {
public ConcreteColleagueA(Mediator mediator) {
super(mediator);
}
@Override
public void action() {
System.out.println("交给中介做A的事情");
}
}
public class ConcreteColleagueB extends Colleague {
public ConcreteColleagueB(Mediator mediator) {
super(mediator);
}
@Override
public void action() {
System.out.println("交给中介做B的事情");
}
}
以电脑为例子。CPU,显卡,内存等零件的交互都是通过主板实现的,而且每个零件只需要做好自己的工作,不需要知道其他零件是什么。所以主板可以作为他们的中介者。
抽象的中介者:
public abstract class Mediator {
public abstract void change(Colleague colleague);
}
具体的中介者,主板:
public class MainBoard extends Mediator {
private CDDevice cdDevice;
private CPU cpu;
private GraphicsCard graphicsCard;
private SoundCard soundCard ;
@Override
public void change(Colleague colleague) {
if (colleague==cdDevice){
handleCD((CDDevice) colleague);
}
if (colleague==cpu){
handleCPU((CPU) colleague);
}
}
private void handleCD(CDDevice cdDevice){
cpu.decodeData(cdDevice.read());
}
private void handleCPU(CPU cpu){
soundCard.playSound(cpu.getDataSound());
graphicsCard.vidoePlay(cpu.getDataVideo());
}
public void setCdDevice(CDDevice cdDevice) {
this.cdDevice = cdDevice;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public void setGraphicsCard(GraphicsCard graphicsCard) {
this.graphicsCard = graphicsCard;
}
public void setSoundCard(SoundCard soundCard) {
this.soundCard = soundCard;
}
}
抽象的零件:
public abstract class Colleague {
public Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
}
具体的零件:
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.change(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.change(this);
}
}
public class GraphicsCard extends Colleague {
public GraphicsCard(Mediator mediator) {
super(mediator);
}
public void vidoePlay(String data){
System.out.println("播放视频:"+data);
}
}
public class SoundCard extends Colleague {
public SoundCard(Mediator mediator) {
super(mediator);
}
public void playSound(String data){
System.out.println("播放音频:"+ data);
}
}
在面向对象编程中,一个类必然会与其他类产生依赖关系,当依赖关系错综复杂时,可以考虑用中介者模式进行解耦。