模式的定義與特點(diǎn)
中介者(Mediator)模式的定義:定義一個中介對象來封裝一系列對象之間的交互,使原有對象之間的耦合松散,且可以獨(dú)立地改變它們之間的交互。中介者模式又叫調(diào)停模式,它是迪米特法則的典型應(yīng)用。
主要優(yōu)點(diǎn)如下
主要缺點(diǎn)是
中介者模式將原本多個對象直接的相互依賴變成了中介者和多個同事類的依賴關(guān)系。當(dāng)同事類越多時,中介者就會越臃腫,變得復(fù)雜且難以維護(hù)。
模式的結(jié)構(gòu)與實(shí)現(xiàn)
中介者模式實(shí)現(xiàn)的關(guān)鍵是找出“中介者”,下面對它的結(jié)構(gòu)和實(shí)現(xiàn)進(jìn)行分析。
1. 模式的結(jié)構(gòu)
示例1,使用interface接口實(shí)現(xiàn)方式
/** * @descript 這是一個中介者類 * 作用1 注冊同事類 * 作用2 負(fù)責(zé)同事類之前的轉(zhuǎn)發(fā)工作 * @author 79197 */public interface Intermediary01 {public void register(Colleague01 colleague);public void transmit(Colleague01 colleague);}public class Intermediary01Implement implements Intermediary01{private List colleagueObj=new ArrayList();//注冊同事類public void register(Colleague01 colleague) {if(!colleagueObj.contains(colleague)) {colleague.setIntermediary01(this);colleagueObj.add(colleague);}}//轉(zhuǎn)發(fā)public void transmit(Colleague01 colleague) {for(Colleague01 obj:colleagueObj) {if(!obj.equals(colleague) && !obj.typeobString.equals(colleague.typeobString)) {obj.receive(colleague.send());}}}}/** * @descript 這是一個同事類 * 作用1 定義不同各類,方便區(qū)分同事類 * 作用2 聚合中介者類 * 作用3 定義和其它同事類交互的類,即發(fā)送消息的類 * 作用4 定義接收其它同事類 * @author 79197 */abstract class Colleague01 {protected Intermediary01 intermediary01;String typeobString;String nameString;String sendStringparameter;public String getSendStringparameter() {return sendStringparameter;}public void setSendStringparameter(String sendStringparameter) {this.sendStringparameter = sendStringparameter;}public Colleague01(String typeobStringparameter,String nameStringparameter) {typeobString=typeobStringparameter;nameString=nameStringparameter;}public void setIntermediary01(Intermediary01 intermediary01temp) {this.intermediary01=intermediary01temp;}public abstract String send();public abstract void receive(String sendcontext);}public class Colleague01A extends Colleague01{public Colleague01A(String typeobStringparameter,String nameStringparameter) {super(typeobStringparameter, nameStringparameter);}String sendStringparameter;public String getSendStringparameter() {return sendStringparameter;}public void setSendStringparameter(String sendStringparameter) {this.sendStringparameter = sendStringparameter;}@Overridepublic String send() {System.out.println(“我是”+super.typeobString+”方,昵稱是:”+super.nameString+”Colleague01B:發(fā)送信息是:”+getSendStringparameter());return “我是”+super.typeobString+”方,昵稱是:”+super.nameString+”Colleague01A:發(fā)送信息是:”+getSendStringparameter();}@Overridepublic void receive(String sendcontext) {System.out.println(“我是”+this.typeobString+”方,昵稱是:”+this.nameString+”Colleague01A:接口信息是:”+sendcontext);}}public class Colleague01B extends Colleague01{public Colleague01B(String typeobStringparameter,String nameStringparameter) {super(typeobStringparameter, nameStringparameter);}String sendStringparameter;public String getSendStringparameter() {return sendStringparameter;}public void setSendStringparameter(String sendStringparameter) {this.sendStringparameter = sendStringparameter;}@Overridepublic String send() {System.out.println(“我是”+super.typeobString+”方,昵稱是:”+super.nameString+”Colleague01B:發(fā)送信息是:”+getSendStringparameter());return “我是”+super.typeobString+”方,昵稱是:”+super.nameString+”Colleague01B:發(fā)送信息是:”+getSendStringparameter();}@Overridepublic void receive(String sendcontext) {System.out.println(“我是”+super.typeobString+”方,昵稱是:”+super.nameString+”Colleague01B:接口信息是:”+sendcontext);}}public class Colleague01C extends Colleague01{public Colleague01C(String typeobStringparameter,String nameStringparameter) {super(typeobStringparameter, nameStringparameter);}String sendStringparameter;public String getSendStringparameter() {return sendStringparameter;}public void setSendStringparameter(String sendStringparameter) {this.sendStringparameter = sendStringparameter;}@Overridepublic String send() {System.out.println(“我是”+super.typeobString+”方,昵稱是:”+super.nameString+”Colleague01B:發(fā)送信息是:”+getSendStringparameter());return “我是”+super.typeobString+”方,昵稱是:”+super.nameString+”Colleague01A:發(fā)送信息是:”+getSendStringparameter();}@Overridepublic void receive(String sendcontext) {System.out.println(“我是”+super.typeobString+”方,昵稱是:”+super.nameString+”Colleague01A:接口信息是:”+sendcontext);}}
測試類
public class IntermediaryTest {public static void main(String[] args) {Colleague01 sellerObj=new Colleague01A(“鏈家賣方”,”小明”);//sellerObj.setSendStringparameter(“長安街有一套上好的房源,正規(guī)3室一廳,感興趣的朋友可以私聊……”);Colleague01 buyerObj1=new Colleague01B(“業(yè)主”,”小張”);Colleague01 buyerObj2=new Colleague01C(“業(yè)主”,”小李”);buyerObj1.setSendStringparameter(“想購買一套房子”);Intermediary01 mediaryObj=new Intermediary01Implement();mediaryObj.register(sellerObj);mediaryObj.register(buyerObj1);mediaryObj.register(buyerObj2);mediaryObj.transmit(buyerObj1);}}
示例2 抽象類的實(shí)現(xiàn)方式
/** * @descript 這是一個中介者類 * 作用1 注冊同事類 * 作用2 負(fù)責(zé)同事類之前的轉(zhuǎn)發(fā)工作 * @author 79197 */abstract class Intermediary01 {abstract void register(Colleague01 colleague);abstract void transmit(Colleague01 colleague);}public class Intermediary01Implement extends Intermediary01{private List colleagueObj=new ArrayList();//注冊同事類public void register(Colleague01 colleague) {if(!colleagueObj.contains(colleague)) {colleague.setIntermediary01(this);colleagueObj.add(colleague);}}//轉(zhuǎn)發(fā)public void transmit(Colleague01 colleague) {for(Colleague01 obj:colleagueObj) {if(!obj.equals(colleague) && !obj.typeobString.equals(colleague.typeobString)) {obj.receive(colleague.send());}}}}