中介者模式 Mediator Pattern
用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
生活中我们经常会需要中介,比如租房找房产中介要比自己找要方便的多。
再如订外卖,如果没有外卖平台,就要保存很多的外卖菜单,即使这样也会因为消息不及时而错过一些新店开业的优惠。而对餐馆来说,想加几样新菜要么是等别人订餐时挨个推销,要么去街头发新的菜单。
在有个了中介-外卖平台以后,这些就变得简单多了,顾客和餐馆都只需要知道中介就可以了,彼此之间不需要关联。
UML图:
代码:
public abstract class Colleague {
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
public abstract void send(String message);
public abstract void receive(String message, String from);
}
public class Customer extends Colleague {
public Customer(Mediator mediator) {
super(mediator);
}
@Override
public void send(String message) {
System.out.println("Customer发出消息: " + message);
this.mediator.send(message, "Customer");
}
@Override
public void receive(String message, String from) {
System.out.println("Customer收到来自" + from + "的消息: " + message);
}
}
public class Restaurant extends Colleague {
public Restaurant(Mediator mediator) {
super(mediator);
}
@Override
public void send(String message) {
System.out.println("Restaurant发出消息: " + message);
this.mediator.send(message, "Restaurant");
}
@Override
public void receive(String message, String from) {
System.out.println("Restaurant收到来自" + from + "的消息: " + message);
}
}
public class DeliveryMan extends Colleague {
public DeliveryMan(Mediator mediator) {
super(mediator);
}
@Override
public void send(String message) {
System.out.println("DeliveryMan发出消息: " + message);
this.mediator.send(message, "DeliveryMan");
}
@Override
public void receive(String message, String from) {
System.out.println("DeliveryMan收到来自" + from + "的消息: " + message);
}
}
public abstract class Mediator {
public abstract void send(String message, String from);
}
public class MeituanMediator extends Mediator {
private Colleague customer;
private Colleague restaurant;
private Colleague deliveryMan;
@Override
public void send(String message, String from) {
switch (from) {
case "Customer":
restaurant.receive(message, from);
break;
case "Restaurant":
deliveryMan.receive(message, from);
break;
case "DeliveryMan":
customer.receive(message, from);
break;
}
}
public void setCustomer(Colleague customer) {
this.customer = customer;
}
public void setRestaurant(Colleague restaurant) {
this.restaurant = restaurant;
}
public void setDeliveryMan(Colleague deliveryMan) {
this.deliveryMan = deliveryMan;
}
}
客户端:
public class Client {
public static void main(String[] args) {
MeituanMediator meituanMediator = new MeituanMediator();
Colleague customer = new Customer(meituanMediator);
Colleague restaurant = new Restaurant(meituanMediator);
Colleague deliveryMan = new DeliveryMan(meituanMediator);
meituanMediator.setCustomer(customer);
meituanMediator.setRestaurant(restaurant);
meituanMediator.setDeliveryMan(deliveryMan);
customer.send("订一份披萨");
restaurant.send("披萨打包完毕");
deliveryMan.send("30分钟后送到");
}
}
Customer发出消息: 订一份披萨
Restaurant收到来自Customer的消息: 订一份披萨
Restaurant发出消息: 披萨打包完毕
DeliveryMan收到来自Restaurant的消息: 披萨打包完毕
DeliveryMan发出消息: 30分钟后送到
Customer收到来自DeliveryMan的消息: 30分钟后送到