首页 > 代码库 > 中介者模式
中介者模式
在生活中,当电脑缺少了一块主板,那会怎么样?如果有人这样问我的话,我就会马上跳出来说“这电脑肯定报废了”,当然这不是重点。假如少了主板电脑还可以用的话,想想,里面的CPU、显卡、声卡、光驱、硬盘等等,不是就要我们自己用线把它们连起来。想想就觉得头疼,那么现在你觉得主板在电脑里扮演着什么角色呢?
在软件的开发过程中,势必会碰到这样一种情况,多个类或多个子系统相互交互,而且交互很繁琐,导致每个类都必须知道他需要交互的类,这样它们的耦合会显得异常厉害。牵一发而动全身又不木有啊!(严重违反低耦合)
好了,既然问题提出来了,那有请我们这期的主角——中介者模式出场吧!
具体过程如下:
1.首先光驱读取光盘上的数据,然后告诉主板,它的状态已经改变了。
2.主板得到光驱的数据,将数据交给CPU进行分析处理。
3.CPU处理完,将数据分成了视频数据和音频数据,通知主板,已将处理好了。
4.主板得到数据,将数据交给显卡和声卡进行输出。
先用几句话说下这些代码,colleague是一个抽象类,是光驱、cpu等的父类,有对中介者mediator的引用,
然后mediator是一个接口,ConcreteMediator实现了它的change方法,有对光驱、cpu等的引用,每当光驱、cpu等自己工作完的时候就去调用中介者的change方法,
然后中介者的change就去做一些调兵遣将的工作比如派显卡和声卡工作,用户就能看到东西了,而这个过程,光驱、cpu等的操作都是隔离开的,没有在内部相互引用,
而都是放在中介者里面引用的!实现了模块间的低耦合!
Mediator
package designMode.mediator2;public interface Mediator { void change(Colleague colleague);}
ConcreteMediator
package designMode.mediator2;public class ConcreteMediator implements Mediator { private CDDriver cdDriver; private CPU cpu; private VideoCard video; private SoundCard sound; public void setCDDriver(CDDriver cdDriver) { this.cdDriver = cdDriver; } public void setCPU(CPU cpu) { this.cpu = cpu; } public void setVideoCard(VideoCard video) { this.video = video; } public void setSoundCard(SoundCard sound) { this.sound = sound; } public void change(Colleague colleague) { if (colleague == cdDriver) { openCDAndReadData((CDDriver) colleague); } else if (colleague == cpu) { openCPU((CPU) colleague); } } private void openCDAndReadData(CDDriver cs) { // 获取光驱读取的数据 String data =http://www.mamicode.com/ cdDriver.getData(); // 把这些数据传递给CPU进行处理 this.cpu.executeData(data); } private void openCPU(CPU cpu) { // 获取数据 String videoData =http://www.mamicode.com/ cpu.getVideioData(); String soundData = cpu.getSoundData(); // 显示数据 this.video.showData(videoData); this.sound.showData(soundData); }}
Colleague
package designMode.mediator2;public abstract class Colleague { private Mediator mediator; public Colleague(Mediator mediator){ this.mediator = mediator; } public Mediator getMediator() { return mediator; }}
CPU
package designMode.mediator2;public class CPU extends Colleague { public CPU(Mediator mediator) { super(mediator); } private String videioData = http://www.mamicode.com/null; private String soundData = http://www.mamicode.com/null; public String getVideioData() { return this.videioData; } public String getSoundData() { return this.soundData; } public void executeData(String data) { String[] ss = data.split(","); this.videioData = http://www.mamicode.com/ss[0]; this.soundData = http://www.mamicode.com/ss[1]; // 通知主板,CPU工作已完成 this.getMediator().change(this); }}
CDDriver
package designMode.mediator2;public class CDDriver extends Colleague { public CDDriver(Mediator mediator) { super(mediator); } private String data = http://www.mamicode.com/null; public String getData() { return this.data; } public void readCD() { //逗号前是视频数据,逗号后是声音数据 this.data = "http://www.mamicode.com/这是视频数据,这是声音数据"; //通知主板,自己的状态反生了改变 this.getMediator().change(this); }}
VideoCard
package designMode.mediator2;public class VideoCard extends Colleague { public VideoCard(Mediator mediator) { super(mediator); // TODO Auto-generated constructor stub } public void showData(String data) { System.out.println("您正在看:" + data); }}
SoundCard
package designMode.mediator2;public class SoundCard extends Colleague { public SoundCard(Mediator mediator) { super(mediator); // TODO Auto-generated constructor stub } public void showData(String data) { System.out.println("您正在听:" + data); }}
ConcreteMediator
package designMode.mediator2;public class ConcreteMediator implements Mediator { private CDDriver cdDriver; private CPU cpu; private VideoCard video; private SoundCard sound; public void setCDDriver(CDDriver cdDriver) { this.cdDriver = cdDriver; } public void setCPU(CPU cpu) { this.cpu = cpu; } public void setVideoCard(VideoCard video) { this.video = video; } public void setSoundCard(SoundCard sound) { this.sound = sound; } public void change(Colleague colleague) { if (colleague == cdDriver) { openCDAndReadData((CDDriver) colleague); } else if (colleague == cpu) { openCPU((CPU) colleague); } } private void openCDAndReadData(CDDriver cs) { // 获取光驱读取的数据 String data =http://www.mamicode.com/ cdDriver.getData(); // 把这些数据传递给CPU进行处理 this.cpu.executeData(data); } private void openCPU(CPU cpu) { // 获取数据 String videoData =http://www.mamicode.com/ cpu.getVideioData(); String soundData = cpu.getSoundData(); // cpu通知声卡显卡工作,用户就可以看到影像了 this.video.showData(videoData); this.sound.showData(soundData); }}
参考自:http://www.cnblogs.com/zxj159/p/3466115.html
中介者模式