中介者模式

阅读《研磨设计模式》笔记。之后若有所感,再补充。
中介者模式目的是:封装对象之间的交互。如果一个对象的状态改变会引起其他相关对象的变化,或一个对象的操作会引起其他对象的后续操作,可以使用中介者来完成这些关联关系。
场景问题:电脑主板与各个部件的关系
示例:
/**
 * @description 中介者对象接口
 * @author liuwei
 * @date 2014-4-18
 * @version 1.0
 */
public interface Mediator {
	/**
	 * 同事对象在自身状态改变的时候来通知中介者对象,意思就是说告诉中介者我的状态改变了,那么其他相应的
	 * 事情也要接着往下做了。
	 * 
	 * @param colleague 同事自身状态,好让中介者对象通过对象实例去获取同事的相应状态
	 */
	public void changed(Colleague colleague);

}

/**
 * @description 所有同事的抽象父类定义
 * @author liuwei
 * @date 2014-4-18
 * @version 1.0
 */
public abstract class Colleague {
	private Mediator mediator;
	public Colleague(Mediator mediator){
		this.mediator = mediator;
	}
	public Mediator getMediator(){
		return this.mediator;
	}
}

/**
 * @description 一个同事类,光驱类
 * @author liuwei
 * @date 2014-4-18
 * @version 1.0
 */
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);
	}
}

/**
 * @description CPUs
 * @author liuwei
 * @date 2014-4-18
 * @version 1.0
 */
public class CPU extends Colleague{
	public CPU(Mediator mediator) {
		super(mediator);
	}
	private String videoData = "";
	private String soundData = "";
	public String getVideoData(){
		return this.videoData;
	}
	public String getSoundData(){
		return this.soundData;
	}
	public void executeData(String data){
		String[] ss = data.split(",");
		this.videoData = ss[0];
		this.soundData = ss[1];
		this.getMediator().changed(this);
	}
}

/**
 * @description 显卡类
 * @author liuwei
 * @date 2014-4-18
 * @version 1.0
 */
public class VideoCard extends Colleague{
	public VideoCard(Mediator mediator) {
		super(mediator);
	}
	public void showData(String data){
		System.out.println("您正在观看的是:" + data);
	}
}

/**
 * @description 声卡
 * @author liuwei
 * @date 2014-4-18
 * @version 1.0
 */
public class SoundCard extends Colleague{
	public SoundCard(Mediator mediator) {
		super(mediator);
	}
	public void soundData(String data){
		System.out.println("画外音:" + data);
	}
}

/**
 * @description
 * @author liuwei
 * @date 2014-4-18
 * @version 1.0
 */
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;
	}
	/* (non-Javadoc)
	 * @see mediator.Mediator#changed(mediator.Colleague)
	 */
	@Override
	public void changed(Colleague colleague) {
		if(colleague == cdDriver){
			this.opeCDDriverReadData(cdDriver);
		}else if(colleague == cpu){
			this.opeCPU(cpu);
		}
	}
	
	private void opeCDDriverReadData(CDDriver cd){
		String data = cd.getData();
		this.cpu.executeData(data);
	}
	
	private void opeCPU(CPU cpu){
		String videoData = cpu.getVideoData();
		String soundData = cpu.getSoundData();
		this.videoCard.showData(videoData);
		this.soundCard.soundData(soundData);
	}
}

/**
 * @description
 * @author liuwei
 * @date 2014-4-18
 * @version 1.0
 */
public class Client {
	public static void main(String[] strs){
		//1:创建中介者:主板对象
		MotherBoard mediator = new MotherBoard();
		//2:创建同事类
		CDDriver cd = new CDDriver(mediator);
		CPU cpu = new CPU(mediator);
		VideoCard videoCard = new VideoCard(mediator);
		SoundCard soundCard = new SoundCard(mediator);
		//3:让中介者知道所有同事
		mediator.setCdDriver(cd);
		mediator.setCpu(cpu);
		mediator.setVideoCard(videoCard);
		mediator.setSoundCard(soundCard);
		//4:开始
		cd.readCD();
	}
}

你可能感兴趣的:(中介者模式)