中介者模式,看字面就知道什么意思了,业务对象太多的时候,每个业务对象又需要建立交流,如果各个业务对象之间都自己去建立交流的话,结果就会很杂乱,比如9个对象,每个对象之间建立一个交流的线,那就是9以2做组合,8*9/2=36条线。但是引入中介者之后,结构就非常清晰了,每个对象要和其他对象交流只需要传达给中介者,中介者会和其他对象交流后返回给你,由中介者对象把每个对象之间的交互封装起来就可以了,此时就只需要9条线就完成了,也减小了耦合度。英文释义:Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring to each other explicitly,and it lets you vary their interaction independently.
示例:
中介类:
public class Mediator{
protected Business1 busines1;
protected Business2 busines2;
protected Business3 busines3;
public Mediator(){
busines1=new Business1();
busines2=new Business2();
busines3=new Business3();
}
public void execute(){
switch(){
......
}
};
}
业务类:
public class Business1{
Mediator mediator=new Mediator();
public void dobusiness1(){
mediator.execute();
}
}