中介者模式
用一个中介对象来封装一系列的对象交互。中介者使得各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。
电脑主板的功能就类似于一个中介者
经典中介者模式UML
例子
经典的中介者模式
package com.tao.YanMoDesignPattern.Mediator.pattern;/*** @Author Mi_Tao* @Date 2023/7/23* @Description 中介者对象接口* @Version 1.0**/
public interface Mediator {/*** 同事对象在自身改变的时候来通知中介者的方法* 让中介者去负责相应的与其他同事对象的交互* @param colleague 同事对象自身,好让中介者对象通过对象实例去获取同事对象的状态*/public void changed(Colleague colleague);
}
package com.tao.YanMoDesignPattern.Mediator.pattern;/*** @Author Mi_Tao* @Date 2023/7/23* @Description* @Version 1.0**/
public class ConcreteMediator implements Mediator{// 持有并维护同事Aprivate ConreteColleagueA conreteColleagueA;// 持有并维护同事Bprivate ConreteColleagueB conreteColleagueB;public void setConreteColleagueA(ConreteColleagueA conreteColleagueA) {this.conreteColleagueA = conreteColleagueA;}public void setConreteColleagueB(ConreteColleagueB conreteColleagueB) {this.conreteColleagueB = conreteColleagueB;}@Overridepublic void changed(Colleague colleague) {// 某个同事发生了变化,通常需要与其他同事交互// 具体协调相应的同事对象来实现协作行为}
}
package com.tao.YanMoDesignPattern.Mediator.pattern;/*** @Author Mi_Tao* @Date 2023/7/23* @Description 同事类抽象类* @Version 1.0**/
public abstract class Colleague {// 持有中介者对象,每一个同事类都知道它的中介者对象private Mediator mediator;public Colleague(Mediator mediator) {this.mediator = mediator;}public void setMediator(Mediator mediator) {this.mediator = mediator;}public Mediator getMediator() {return mediator;}}
package com.tao.YanMoDesignPattern.Mediator.pattern;/*** @Author Mi_Tao* @Date 2023/7/23* @Description* @Version 1.0**/
public class ConreteColleagueA extends Colleague{public ConreteColleagueA(Mediator mediator) {super(mediator);}/*** 示意方法,执行某些业务功能*/public void someOperation(){// 在需要跟其他同事通信的时候,通知中介者对象getMediator().changed(this);}
}
package com.tao.YanMoDesignPattern.Mediator.pattern;/*** @Author Mi_Tao* @Date 2023/7/23* @Description* @Version 1.0**/
public class ConreteColleagueB extends Colleague{public ConreteColleagueB(Mediator mediator) {super(mediator);}/*** 示意方法,执行某些业务功能*/public void someOperation(){// 在需要跟其他同事通信的时候,通知中介者对象getMediator().changed(this);}
}
例子:电脑主板和各个部件关系
package com.tao.YanMoDesignPattern.Mediator.case1_mainbox;/*** @Author Mi_Tao* @Date 2023/7/23* @Description 中介者对象接口* @Version 1.0**/
public interface Mediator {/*** 同事对象在自身改变的时候来通知中介者的方法* 让中介者去负责相应的与其他同事对象的交互* @param colleague 主板*/public void changed(Colleague colleague);
}
驱动
package com.tao.YanMoDesignPattern.Mediator.case1_mainbox;/*** @Author Mi_Tao* @Date 2023/7/23* @Description 光驱* @Version 1.0**/
public class CDDriver extends Colleague {public CDDriver(Mediator mediator) {super(mediator);}// 光驱读出来数据private String date = "";public String getDate() {return date;}public void readCD(){// 逗号前是视频显示的数据,逗号后是声音this.date ="设计模式,值的好好研究!";// 通知主板,自己的状态发生了改变this.getMediator().changed(this);}
}
CPU
package com.tao.YanMoDesignPattern.Mediator.case1_mainbox;/*** @Author Mi_Tao* @Date 2023/7/23* @Description 光驱* @Version 1.0**/
public class CDDriver extends Colleague {public CDDriver(Mediator mediator) {super(mediator);}// 光驱读出来数据private String date = "";public String getDate() {return date;}public void readCD(){// 逗号前是视频显示的数据,逗号后是声音this.date ="设计模式,值的好好研究!";// 通知主板,自己的状态发生了改变this.getMediator().changed(this);}
}
声卡
package com.tao.YanMoDesignPattern.Mediator.case1_mainbox;/*** @Author Mi_Tao* @Date 2023/7/23* @Description 光驱* @Version 1.0**/
public class SoundCard extends Colleague {public SoundCard(Mediator mediator) {super(mediator);}public void showData(String data){System.out.println("画外音: "+data );}
}
显卡
package com.tao.YanMoDesignPattern.Mediator.case1_mainbox;/*** @Author Mi_Tao* @Date 2023/7/23* @Description 光驱* @Version 1.0**/
public class ViedoCard extends Colleague {public ViedoCard(Mediator mediator) {super(mediator);}public void showData(String data){System.out.println("您正在观看的是: "+data );}
}
中介者类(主板)最核心的,封装其他对象,负责其他对象之间的交互(统筹兼顾)
package com.tao.YanMoDesignPattern.Mediator.case1_mainbox;/*** @Author Mi_Tao* @Date 2023/7/23* @Description* @Version 1.0**/
public class MainBoard implements Mediator{// 需要知道要交互的同事类private CPU cpu = null;private CDDriver cdDriver = null;private SoundCard soundCard = null;private ViedoCard viedoCard = null;@Overridepublic void changed(Colleague colleague) {if (colleague == cdDriver){// 光驱读取数据this.opeCDDriverReadDate((CDDriver)colleague );}if (colleague == cpu){// 表示CPUU 处理完了this.opeCPU((CPU)colleague);}}/*** 处理光驱读取数据以后与其他对象的交互** @param cd cd*/private void opeCDDriverReadDate(CDDriver cd){// 1 先获取光驱读取的数据String date = cd.getDate();// 2 把这些数据传递给CPU 处理this.cpu.executeData(date);}public void opeCPU(CPU cpu){// 1先获取CPU处理后的数据String videoDate = cpu.getVideoDate();String soudDate = cpu.getSoudDate();// 2把这些数据传递给显卡和声卡this.viedoCard.showData(videoDate);this.soundCard.showData(soudDate);}public void setCpu(CPU cpu) {this.cpu = cpu;}public void setCdDriver(CDDriver cdDriver) {this.cdDriver = cdDriver;}public void setSoundCard(SoundCard soundCard) {this.soundCard = soundCard;}public void setViedoCard(ViedoCard viedoCard) {this.viedoCard = viedoCard;}
}
测试
package com.tao.YanMoDesignPattern.Mediator.case1_mainbox;/*** @Author Mi_Tao* @Date 2023/7/23* @Description* @Version 1.0**/
public class Client {public static void main(String[] args) {// 1 创建中介者 -- 主板对象MainBoard mediator = new MainBoard();// 2 创建同事类CDDriver cdDriver = new CDDriver(mediator);CPU cpu = new CPU(mediator);SoundCard soundCard = new SoundCard(mediator);ViedoCard viedoCard = new ViedoCard(mediator);// 3 让中介者知道所有的同事mediator.setCpu(cpu);mediator.setCdDriver(cdDriver);mediator.setSoundCard(soundCard);mediator.setViedoCard(viedoCard);// 4 开始看电影 把光盘放入光驱,光驱开始读盘cdDriver.readCD();}
}
中介者的本质:封装交互(跟外观者模式有点像)