什么是中介者模式
用一个中介对象封装一系列的对象 交互,中介者使各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立地改变它 们之间的交互,减少类之间的依赖
中介者模式适用场景
在以下情况下可以考虑使用中介者模式:
- 系统中对象之间存在复杂的引用关系,系统结构混乱且难以理解。
- 一个对象由于引用了其他很多对象并且直接和这些对象通信,导致难以复用该对象。
- 想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。可以通过引入中介者类来实现,在中介者中定义对象交互的公共行为,如果需要改变行为则可以增加新的具体 中介者类
中介者模式的分类
标准中介者
package com.lzhsite.technology.designPattern.mediator.ComputerDemo; /** * 期望实现的效果:驱动执行完之后执行cpu,cpu执行完之后执行声音和音频 * @author lzhcode * */ public class ClientForComputerDemo { public static void main(String[] args) { MainBoard mainBoard = new MainBoard(); CDDriver cdDriver = new CDDriver(mainBoard); CPU cpu = new CPU(mainBoard); VideoCard videoCard = new VideoCard(mainBoard); SoundCard soundCard = new SoundCard(mainBoard); mainBoard.setCdDriver(cdDriver); mainBoard.setCpu(cpu); mainBoard.setVideoCard(videoCard); mainBoard.setSoundCard(soundCard); cdDriver.readCD(); } }
package com.lzhsite.technology.designPattern.mediator.ComputerDemo; public interface Mediator { /** * 同事对象在自身发生改变的时候,通知中介者的方法 * 让中介者去负责相应同事的交互 * 同事对象在自身,好让中介者对象实例去获取同事对象的状态 * @param colleague */ public void changed(Colleague colleague); }
package com.lzhsite.technology.designPattern.mediator.ComputerDemo; /*** * 相当于中介类 **/ public class MainBoard implements Mediator { protected CPU cpu; private CDDriver cdDriver; private VideoCard videoCard; private SoundCard soundCard; 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; } public void changed(Colleague colleague) { if (colleague == cdDriver) { this.operateCDDriverReadData((CDDriver) colleague); } else if (colleague == cpu) { this.operateCPU((CPU) colleague); } } public void operateCDDriverReadData(CDDriver cdDriver) { String data = cdDriver.getData(); this.cpu.executeData(data); } public void operateCPU(CPU cpu) { String videoData = cpu.getVideoData(); String soundData = cpu.getSoundData(); this.videoCard.showData(videoData); this.soundCard.soundData(soundData); } }
package com.lzhsite.technology.designPattern.mediator.ComputerDemo; public abstract class Colleague { private final Mediator mediator; protected Colleague(Mediator mediator) { this.mediator = mediator; } public Mediator getMediator() { return mediator; } }
package com.lzhsite.technology.designPattern.mediator.ComputerDemo; public class CPU extends Colleague { private String videoData; private String soundData; protected CPU(Mediator mediator) { super(mediator); } public String getVideoData() { return videoData; } public String getSoundData() { return soundData; } public void executeData(String data) { String[] ss = data.split(","); this.videoData = ss[0]; this.soundData = ss[1]; this.getMediator().changed(this); } }
package com.lzhsite.technology.designPattern.mediator.ComputerDemo; public class SoundCard extends Colleague { protected SoundCard(Mediator mediator) { super(mediator); } public void soundData(String data) { System.out.println("画外音:" + data); } }
package com.lzhsite.technology.designPattern.mediator.ComputerDemo; public class VideoCard extends Colleague { protected VideoCard(Mediator mediator) { super(mediator); } public void showData(String data) { System.out.println("您正在观看的是:" + data); } }
package com.lzhsite.technology.designPattern.mediator.ComputerDemo; public class CDDriver extends Colleague { private String data; protected CDDriver(Mediator mediator) { super(mediator); } public String getData() { return data; } public void readCD() { this.data = "Video Data, Sound Data"; this.getMediator().changed(this); } }
广义中介者
package com.lzhsite.technology.designPattern.mediator.ColleagueDemo; public class ClientForColleagueDemo { public static void main(String[] args) { AbstractMediator mediator = new Mediator(); ColleagueA colleagueA = new ColleagueA(); ColleagueB colleagueB = new ColleagueB(); mediator.addColleague("ColleagueA", colleagueA); mediator.addColleague("ColleagueB", colleagueB); colleagueA.self(); colleagueA.out(); System.out.println("合作愉快,任务完成"); System.out.println(); colleagueB.self(); colleagueB.out(); System.out.println("合作愉快,任务完成"); } }
package com.lzhsite.technology.designPattern.mediator.ColleagueDemo; public class AbstractColleague { protected AbstractMediator mediator; public void setMediator(AbstractMediator mediator) { this.mediator = mediator; } }
package com.lzhsite.technology.designPattern.mediator.ColleagueDemo; import java.util.Hashtable; public abstract class AbstractMediator { protected Hashtable<String, AbstractColleague> colleagues = new Hashtable<String, AbstractColleague>(); public void addColleague(String name, AbstractColleague colleague) { //在这里把自己注册进中介mediator,与其取得联系 colleague.setMediator(this); this.colleagues.put(name, colleague); } public void deleteColleague(String name) { this.colleagues.remove(name); } public abstract void execute(String name, String method); }
package com.lzhsite.technology.designPattern.mediator.ColleagueDemo; public class Mediator extends AbstractMediator { public void execute(String name, String method) { if ("self".equals(method)) { if ("ColleagueA".equals(name)) { ColleagueA colleagueA = (ColleagueA) super.colleagues.get("ColleagueA"); colleagueA.self(); } else { ColleagueB colleagueB = (ColleagueB) super.colleagues.get("ColleagueB"); colleagueB.self(); } } else { if ("ColleagueA".equals(name)) { ColleagueA colleagueA = (ColleagueA) super.colleagues.get("ColleagueA"); colleagueA.out(); } else { ColleagueB colleagueB = (ColleagueB) super.colleagues.get("ColleagueB"); colleagueB.out(); } } } }
package com.lzhsite.technology.designPattern.mediator.ColleagueDemo; public class ColleagueA extends AbstractColleague { public void self() { System.out.println("同事A --> 做好自己份内的事情..."); } public void out() { System.out.println("同事A --> 请求同事B 做好份内的事情"); super.mediator.execute("ColleagueB", "self"); } }
package com.lzhsite.technology.designPattern.mediator.ColleagueDemo; public class ColleagueB extends AbstractColleague { public void self() { System.out.println("同事B --> 做好自己份内的事情..."); } public void out() { System.out.println("同事B --> 请求同事A 做好份内的事情"); super.mediator.execute("ColleagueA", "self"); } }