定义:
中介者模式的角色组成:
中介者模式的 UML 类图:
情景案例:生活中的租房情景就是中介者模式的体现,房东和租客以及租房中介分别充当了同事和中介者角色:房东将房屋托管给租房中介,而租客从租房中介那边获取房源信息,租房中介充当房东和租客之间的中介者。
抽象中介者 Mediator
:
public interface Mediator {
void contact(String message, Person person);
}
抽象同事 Person
:
public abstract class Person {
protected String name;
protected Mediator mediator;
public Person(String name, Mediator mediator) {
this.name = name;
this.mediator = mediator;
}
public abstract void contact(String message);
public abstract void getMessage(String message);
}
具体同事房东 LandLord
:
public class LandLord extends Person {
public LandLord(String name, Mediator mediator) {
super(name, mediator);
}
public void contact(String message) {
mediator.contact(message, this);
}
public void getMessage(String message) {
System.out.println("房东" + name + "获取的信息是:" + message);
}
}
具体同事租客 Tenant
:
public class Tenant extends Person {
public Tenant(String name, Mediator mediator) {
super(name, mediator);
}
public void contact(String message) {
mediator.contact(message, this);
}
public void getMessage(String message) {
System.out.println("租客" + name + "获取的信息是:" + message);
}
}
具体中介者租房机构 MediatorOrgan
:
public class MediatorOrgan implements Mediator {
private LandLord landLord;
private Tenant tenant;
public LandLord getLandLord() {
return landLord;
}
public void setLandLord(LandLord landLord) {
this.landLord = landLord;
}
public Tenant getTenant() {
return tenant;
}
public void setTenant(Tenant tenant) {
this.tenant = tenant;
}
@Override
public void contact(String message, Person person) {
if (person == landLord) {
tenant.getMessage(message);
} else {
landLord.getMessage(message);
}
}
}
调用的客户端 Client
:
public class Client {
public static void main(String[] args) {
MediatorOrgan mediatorOrgan = new MediatorOrgan();
LandLord landLord = new LandLord("包租婆", mediatorOrgan);
Tenant tenant = new Tenant("小伙汁", mediatorOrgan);
mediatorOrgan.setLandLord(landLord);
mediatorOrgan.setTenant(tenant);
tenant.contact("我需要租包租婆的房子");
landLord.contact("小伙汁,我的房子还在可以租给你");
}
}
中介者模式的优点:
中介者模式的缺点:
中介者模式的适用场景:
中介者模式在JDK源码
java.util.Timer
中的应用
Timer
类用于创建定时器任务。它充当了一个中介者的角色,负责协调计时器任务的创建和执行。通过 Timer
类,我们可以创建定时任务,设置任务的执行时间和间隔,并且可以取消已经创建的任务。
定时器Timer
类部分源码如下:
public class Timer {
// ...
public void schedule(TimerTask task, long delay) {
if (delay < 0)
throw new IllegalArgumentException("Negative delay.");
sched(task, System.currentTimeMillis()+delay, 0);
}
private void sched(TimerTask task, long time, long period) {
if (time < 0)
throw new IllegalArgumentException("Illegal execution time.");
// Constrain value of period sufficiently to prevent numeric
// overflow while still being effectively infinitely large.
if (Math.abs(period) > (Long.MAX_VALUE >> 1))
period >>= 1;
synchronized(queue) {
if (!thread.newTasksMayBeScheduled)
throw new IllegalStateException("Timer already cancelled.");
synchronized(task.lock) {
if (task.state != TimerTask.VIRGIN)
throw new IllegalStateException(
"Task already scheduled or cancelled");
task.nextExecutionTime = time;
task.period = period;
task.state = TimerTask.SCHEDULED;
}
queue.add(task);
if (queue.getMin() == task)
queue.notify();
}
}
// ...
}
无论什么样的任务都被加入一个队列中按顺序执行,该队列中的所有对象都称为“同事”,同事之间的通信都是通过Timer
来协调完成的,Timer
承担了中介者的角色。