用途
中介者模式是一种
行为型模式。
结构
图-中介者模式结构图
Mediator : 为 Colleague 对象定义一个交流接口。
abstract
class Mediator {
public abstract void Send(String message, Colleague colleague);
}
public abstract void Send(String message, Colleague colleague);
}
ConcreteMediator : 实现 Mediator 中的交流接口。 这个类中需要了解并维护所有的 colleague 对象。
class ConcreteMediator
extends Mediator {
private ConcreteColleague1 colleague1;
private ConcreteColleague2 colleague2;
public void setColleague1(ConcreteColleague1 colleague1) {
this.colleague1 = colleague1;
}
public void setColleague2(ConcreteColleague2 colleague2) {
this.colleague2 = colleague2;
}
@Override
public void Send(String message, Colleague colleague) {
if (colleague == colleague1) {
colleague2.Notify(message);
} else if (colleague == colleague2){
colleague1.Notify(message);
} else {
System.out.println("Error!");
}
}
}
private ConcreteColleague1 colleague1;
private ConcreteColleague2 colleague2;
public void setColleague1(ConcreteColleague1 colleague1) {
this.colleague1 = colleague1;
}
public void setColleague2(ConcreteColleague2 colleague2) {
this.colleague2 = colleague2;
}
@Override
public void Send(String message, Colleague colleague) {
if (colleague == colleague1) {
colleague2.Notify(message);
} else if (colleague == colleague2){
colleague1.Notify(message);
} else {
System.out.println("Error!");
}
}
}
Colleague组 : 每个 Colleague 对象应该知道它的 Mediator 对象,但不知道其他同事对象。它只能联系 Mediator 对象。
abstract
class Colleague {
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
public void Send(String message) {
mediator.Send(message, this);
}
public abstract void Notify(String message);
}
class ConcreteColleague1 extends Colleague {
public ConcreteColleague1(Mediator mediator) {
super(mediator);
}
@Override
public void Notify(String message) {
System.out.println("同事1得到信息:" + message);
}
}
class ConcreteColleague2 extends Colleague {
public ConcreteColleague2(Mediator mediator) {
super(mediator);
}
@Override
public void Notify(String message) {
System.out.println("同事2得到信息:" + message);
}
}
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
public void Send(String message) {
mediator.Send(message, this);
}
public abstract void Notify(String message);
}
class ConcreteColleague1 extends Colleague {
public ConcreteColleague1(Mediator mediator) {
super(mediator);
}
@Override
public void Notify(String message) {
System.out.println("同事1得到信息:" + message);
}
}
class ConcreteColleague2 extends Colleague {
public ConcreteColleague2(Mediator mediator) {
super(mediator);
}
@Override
public void Notify(String message) {
System.out.println("同事2得到信息:" + message);
}
}
测试代码
public
class MediatorPattern {
public static void main(String[] args) {
ConcreteMediator mediator = new ConcreteMediator();
ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);
mediator.setColleague1(colleague1);
mediator.setColleague2(colleague2);
colleague1.Send("How are you?");
colleague2.Send("Fine, thank you. And you?");
colleague1.Send("I'm fine. Thankes.");
}
}
public static void main(String[] args) {
ConcreteMediator mediator = new ConcreteMediator();
ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);
mediator.setColleague1(colleague1);
mediator.setColleague2(colleague2);
colleague1.Send("How are you?");
colleague2.Send("Fine, thank you. And you?");
colleague1.Send("I'm fine. Thankes.");
}
}
运行结果
同事2得到信息:How are you?
同事1得到信息:Fine, thank you. And you?
同事2得到信息:I'm fine. Thankes.
同事1得到信息:Fine, thank you. And you?
同事2得到信息:I'm fine. Thankes.
应用场景
中介者模式一般用于定义良好但是交互较为负责的对象集合。产生的相互依赖关系是无结构化的并且难以理解。
和很多其他对象有引用关系以及交互的对象很难被重用。
想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。
本文转自静默虚空博客园博客,原文链接:http://www.cnblogs.com/jingmoxukong/p/4238479.html,如需转载请自行联系原作者