1.定义:中介者模式,也称为调停者模式。封装一系列的对象交互,中介者使各对象不需要显示的相互作用,从而使其耦合松散,而且可以独立的改变他们之间的交互。Define an object that encapsulates how a set of objects interact. Meditator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
2.简单代码示例
public abstract class ZhongJieZhe { //抽象的中介者类
protected Colleague1 collegue1; //可以定义多可业务对象,单业务对象较多中介者模式的特征也就越显示出来
protected Colleague2 collegue2; //中介者使各个对象不需要显示的交互,都由中介者来完成,从而使其耦合松散。
protected Colleague1 colleague3;
public Colleague1 getCollegue1() { //中介者业务对象的注入
return collegue1;
}
public void setCollegue1(Colleague1 collegue1) {
this.collegue1 = collegue1;
}
public Colleague2 getCollegue2() {
return collegue2;
}
public void setCollegue2(Colleague2 collegue2) {
this.collegue2 = collegue2;
}
public abstract void SendToOtherCollegue(); //中介者服务
public abstract void MakeSomebodyDoSomething(String str );
}
public class ZhongJie extends ZhongJieZhe{ //实际中介者类 private static int cost=0; @Override public void SendToOtherCollegue() { // TODO Auto-generated method stub System.out.println("先声明此次服务我中介者要收"+5); super.getCollegue2().hehe(); cost+=5; } @Override public void MakeSomebodyDoSomething(String str) { // TODO Auto-generated method stub int i=Integer.valueOf(str.substring(str.indexOf("[")+1,str.indexOf("[")+3)); System.out.println("先声明此次服务我中介者要收"+6); super.getCollegue1().add(i); cost+=6; } public void sum() { System.out.println("我中介者目前赚了大概"+cost); }}
public class Colleague1 { //具体的业务对象类1,注意如果多个业务对象具有较多相似业务,可以定义更高层的抽象类或接口。 private static int count=10; protected ZhongJieZhe zhongJieZhe; public Colleague1(ZhongJieZhe zhongJieZhe) //中介者注入,本类需要与其他类交互的地方,通过中介者来与其他类沟通。 { this.zhongJieZhe=zhongJieZhe; } public void sub(int i) //本类的业务逻辑 { count-=i; System.out.println("销售成功 count="+count); if(count<8) //如果count太小 则委托中介处理业务。 { zhongJieZhe.SendToOtherCollegue(); } } public void add(int i) { System.out.println("中介者告诉我进货成功了,我不知道谁进的货。管他呢!货好就行"); count+=i; System.out.println("进货成功 count="+count); } public void DoOtherThings() { System.out.println("这是Colleague1类,我要做我自己的事情"); } }public class Colleague2 { //具体的业务对象类2 ZhongJieZhe zhongJieZhe; private int cinnt=0; public Colleague2(ZhongJieZhe zhongJieZhe) { this.zhongJieZhe=zhongJieZhe; } public void hehe() { System.out.println("我是Colleague2类,中介者通知我有事情做了。我要去进货了,兄弟们"); haha(); zhongJieZhe.MakeSomebodyDoSomething("告诉他,我已经进货回来了,进了["+cinnt+"]个货"); } public void haha() { cinnt=10; } public void DoOtherThings() { System.out.println("这是Colleague2类,我要做我自己的事情"); } }
System.out.println(); System.out.println("************中介者模式************"); ZhongJieZhe zhongJieZhe=new ZhongJie(); Colleague1 colleague1=new Colleague1(zhongJieZhe); Colleague2 colleague2=new Colleague2(zhongJieZhe); zhongJieZhe.setCollegue1(colleague1); zhongJieZhe.setCollegue2(colleague2); colleague1.sub(1); colleague1.DoOtherThings(); colleague1.sub(2);************中介者模式************销售成功 count=9这是Colleague1类,我要做我自己的事情销售成功 count=7先声明此次服务我中介者要收5我是Colleague2类,中介者通知我有事情做了。我要去进货了,兄弟们先声明此次服务我中介者要收6中介者告诉我进货成功了,我不知道谁进的货。管他呢!货好就行进货成功 count=17此示例中通过中介者来完成两个对象之间的交互。两个对象的交互比较简单、清晰。但是如果有n个对象之间发生交互,假如两两之间发生调用,则有n(n-1)中调用方式,类之间的关系十分复杂。通过中介者来完成多个对象之间的交互都通过中介者模式完成,从而将复杂的网络结构变成星型结构,使类间耦合变少,结构清晰。
中介者模式的不足就是,过多的对象调用关系使中介者里面的逻辑复杂,如果中介者处理不过来就会变成瓶颈。所以中介者应该量力而为。