中介者模式
Mediator模式
行为型模式之一
类之间的交互行为被统一放在Mediator对象中,对象通过Mediator对象和其它对象进行交互,Mediator对象起着控制器的作用
当对象之间有过多的交互行为时,这显然是高耦的,从设计理念上来说是不推荐的
先来看看个小案例
public abstract class Person {
public String getName() {
return name;
}
public String getCondition() {
return condition;
}
String name;
String condition;
public Person(String name, String condition) {
this.name = name;
this.condition = condition;
}
public void getLover(Person person){};
}
public class Man extends Person{
public Man(String name, String condition) {
super(name, condition);
}
@Override
public void getLover(Person person) {
if (person instanceof Man) System.out.println("偶不是同性恋...");
else if (person.getCondition().equals(this.getCondition()))
System.out.println("这是命运的安排: " + this.getName() + " " + person.getName());
else System.out.println("对不起,你是个好人");
}
}
public class Woman extends Person{
public Woman(String name, String condition) {
super(name, condition);
}
@Override
public void getLover(Person person) {
if (person instanceof Woman) System.out.println("啊,百合爱了爱了,但不优先考虑哦");
else if (person.getCondition().equals(this.getCondition()))
System.out.println("这是命运的安排: " + this.getName() + " " + person.getName());
else System.out.println("对不起,你是个好人");
}
}
/**
* 这是命运的安排: liangye linan
* 啊,百合爱了爱了,但不优先考虑哦
*/
public class Client {
public static void main(String[] args) {
Person ly = new Man("liangye", "合适就好");
Person ln = new Woman("linan","合适就好");
Person lj = new Woman("ll","随缘吧");
ly.getLover(ln);
ln.getLover(lj);
}
}
- 可见,在Man的getLover()里频繁调用别的类的getCondition(),这是不推荐的
结构图
public abstract class Person {
public String getName() {
return name;
}
public String getCondition() {
return condition;
}
public Person(Mediator mediator, String name, String condition) {
this.mediator = mediator;
this.name = name;
this.condition = condition;
}
public Mediator getMediator() {
return mediator;
}
private Mediator mediator;
private String name;
private String condition;
public void getLover(Person person){};
}
public class Man extends Person {
public Man(Mediator mediator, String name, String condition) {
super(mediator, name, condition);
}
public void getLover(Person person){
//像中介公司注册自己
this.getMediator().setMan(this);
this.getMediator().getLover(person);
};
}
public class Woman extends Person {
public Woman(Mediator mediator, String name, String condition) {
super(mediator, name, condition);
}
public void getLover(Person person){
//像中介公司注册自己
this.getMediator().setWoman(this);
this.getMediator().getLover(person);
};
}
/**
* 这里直接使用具体的中介者了
*/
public class Mediator {
private Man man;
private Woman woman;
public void setMan(Man man) {
this.man = man;
}
public void setWoman(Woman woman) {
this.woman = woman;
}
public void getLover(Person person) {
if (person instanceof Man)
this.setMan((Man)person);
else this.setWoman((Woman)person);
if (man == null || woman == null)
System.out.println("我不是同性恋!");
else {
if (this.man.getCondition().equals(this.woman.getCondition()))
System.out.println("这是命运的安排: " + this.man.getName() + " " + this.woman.getName());
else System.out.println("对不起,你是个好人");
}
//清空当前信息
this.setWoman(null);
this.setMan(null);
}
}
/**
* 运行结果
* 这是命运的安排: liangye linan
* 我不是同性恋!
* 这是命运的安排: liangye linan
*/
public class Client {
public static void main(String[] args) {
Mediator mediator = new Mediator();
Person ly = new Man(mediator, "liangye", "合适就好");
Person ln = new Woman(mediator, "linan", "合适就好");
Person ll = new Woman(mediator ,"lw", "随缘");
ln.getLover(ly);
ll.getLover(ln);
ly.getLover(ln);
}
}
优点
- 将系统按照功能划分成更小的对象,符合类的最小设计原则
- 对关联对象的集中控制
- 减少类的耦合程度,明确类之间的相互关系
- 当类之间的关系过于复杂时,其中任何一个类的修改都会影响到其他类,不符合类的设计的开闭原则
- Mediator模式将原来相互依存的多对多的类之间的关系简化为Mediator控制类与其他关联类之前的一对多的关系
- 这样的话,当其中一个类修改时,可以对其它关联类不产生影响(即使有修改,也集中在Mediator控制类)