中介者模式是指一种用一个中介对象来封装一系列的对象交互。中介者使个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。
适合中介者模式的情景如下:
- 许多对象以复杂的方式交互,所导致的依赖关系使系统难以维护
- 一个对象引用其他很多对象,导致难以复用该对象
中介者模式解决问题的思路很简单,就是通过引入一个中介对象,让其他对象只与中介对象交互,而中介对象知道如何和其他所有对象的交互,这样对象之间的交互关系就没有了,从而实现了对象之间的解耦。
具体实例:
以房屋中介功能为例,实现的功能是:
- 租客发布租房信息到房屋中介,房屋中介将收到的信息发布给所有的房东
- 房东发布信息到房屋中介,房屋中介将收到的信息发布给租客
(1)租客
receive() 方法用于租客接收来自中介者的信息;send() 方法用于租客向中介者发信息。
public class Renter {
String account;//账号
String name;
Mediator me;//用户和中介者通信
public void receive(String msg) {
System.out.println(account + "\t" +name + "receive:");
System.out.println("\t" +msg);
}
public void send(String msg) {
me.send(this,msg);
}
public Renter(String account, String name, Mediator me) {
this.account = account;
this.name = name;
this.me = me;
}
public String getAccount() {
return account;
}
public String getName() {
return name;
}
}
(2)房东
public class Saler {
String account;
String name;
Mediator me;
public void receive(String msg) {
System.out.println(account + "\t" +name + "receive:");
System.out.println("\t" +msg);
}
public void send(String msg) {
me.send(this,msg);
}
public Saler(String account, String name, Mediator me) {
this.account = account;
this.name = name;
this.me = me;
}
public String getAccount() {
return account;
}
public String getName() {
return name;
}
}
(3)中介
因为中介必须知道所有用户的信息,所以定义了租客和房东的map集合,send() 方法用于接收某一方的信息,并且向另一方的所有对象广播出去。
Setset = m2.keySet();//拿到map中的key存入set集合中
public class Mediator {
Map m = new HashMap<>();
Map m2 = new HashMap<>();
void addRenter(Renter r) {
m.put(r.getAccount(),r);
}
void addSaler(Saler s) {
m2.put(s.getAccount(),s);
}
public void send(Renter renter, String msg) {
System.out.println("来自租客"+renter.account+"-"+renter.getName());
System.out.println("\t"+msg);
//拿到房东的set集合
Set set = m2.keySet();
Iterator iterable = set.iterator();
//遍历房东set集合
while (iterable.hasNext()) {
String key = iterable.next(); //拿到房东的账号
Saler sa = m2.get(key);//根据账号key拿到房东信息
//调用房东的receive方法,将租客的信息发送给房东
sa.receive(renter.getAccount()+"-"+renter.getName()+"-"+msg);
}
}
public void send(Saler saler, String msg) {
System.out.println("来自租客"+saler.account+"-"+saler.getName());
System.out.println("\t"+msg);
//拿到房东的set集合
Set set = m.keySet();
Iterator iterable = set.iterator();
//遍历房东set集合
while (iterable.hasNext()) {
String key = iterable.next(); //拿到租客的账号
Renter sa = m.get(key);//根据账号key拿到租客信息
//调用租客的receive方法,将房东的信息发送给租客
sa.receive(saler.getAccount()+"-"+saler.getName()+"-"+msg);
}
}
}
(4)测试类
public class Test {
public static void main(String[] args) {
Mediator mediator = new Mediator();
//租客
Renter renter1 = new Renter("1001","zhangyin",mediator);
Renter renter2 = new Renter("1002","minmin",mediator);
Renter renter3 = new Renter("1003","sisi",mediator);
mediator.addRenter(renter1);
mediator.addRenter(renter2);
mediator.addRenter(renter3);
//租户
Saler saler1 = new Saler("2001","dongzi",mediator);
Saler saler2 = new Saler("2002","haozi",mediator);
Saler saler3 = new Saler("2003","xiangzi",mediator);
mediator.addSaler(saler1);
mediator.addSaler(saler2);
mediator.addSaler(saler3);
renter2.send("Minmin要租房!");
System.out.println("====================================");
saler2.send("浩子有房要出租!");
}
}
测试结果:
来自租客1002-minmin
Minmin要租房!
2003 xiangzi receive:
1002-minmin-Minmin要租房!
2002 haozi receive:
1002-minmin-Minmin要租房!
2001 dongzi receive:
1002-minmin-Minmin要租房!
====================================
来自租客2002-haozi
浩子有房要出租!
1003 sisi receive:
2002-haozi-浩子有房要出租!
1002 minmin receive:
2002-haozi-浩子有房要出租!
1001 zhangyin receive:
2002-haozi-浩子有房要出租!