目录
定义
结构
案例
优点
缺点
使用场景
又叫调停模式,定义一个中介角色来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。
中介者模式包含以下主要角色:
//抽象中介类
public abstract class Mediator {
public abstract void contact(String message,Person person);
public abstract void getMessage(Person person);
}
具体中介类
public class MediatorStructure extends Mediator {
private List houseOwners = new ArrayList<>();
private List tenants = new ArrayList<>();
//购房者的要求
private Map> tenantRequest = new HashMap<>();
//房主的服务
private Map> houseOwnerResponse = new HashMap<>();
public List getHouseOwners() {
return houseOwners;
}
public void setHouseOwners(HouseOwner houseOwner) {
houseOwners.add(houseOwner);
}
public List getTenants() {
return tenants;
}
public void setTenants(Tenant tenant) {
tenants.add(tenant);
}
@Override
public void contact(String message, Person person) {
List list = null;
if (person instanceof HouseOwner) {
//说明是房主联系中介,中介需要把消息给对应的购房者
//首先判断信息map中有没有这个房主信息
HouseOwner houseOwner = (HouseOwner) person;
list = houseOwnerResponse.get(person);
if (list == null || list.size() <= 0) {
//说明没有这个房主的信息
list = new ArrayList<>();
list.add(message);
houseOwnerResponse.put(houseOwner, list);
} else {
if (list.contains(message)) {
//说明已经存在这个消息了
return;
}
list.add(message);
houseOwnerResponse.put(houseOwner, list);
}
} else {
//说明购房者发起了消息
Tenant tenant = (Tenant) person;
list = tenantRequest.get(tenant);
if (list == null || list.size() <= 0) {
list = new ArrayList<>();
//说明没有这个购房者的信息
list.add(message);
tenantRequest.put(tenant, list);
} else {
if (list.contains(message)) {
//说明已经存在这个消息了
return;
}
list.add(message);
tenantRequest.put(tenant, list);
}
}
}
public void getMessage(Person person) {
Set set = new HashSet<>();
if (person instanceof HouseOwner) {
//房主主动获取购房者的需求
for (Tenant tenant : tenantRequest.keySet()) {
List list = tenantRequest.get(tenant);
set.addAll(list);
}
((HouseOwner) person).getMessage(set);
} else {
//购房者只需要知道符合自己的就可以
List list = tenantRequest.get(person);
for (String s : list) {
for (HouseOwner houseOwner : houseOwnerResponse.keySet()) {
List response = houseOwnerResponse.get(houseOwner);
if (response.contains(s)) {
set.add(houseOwner.name);
}
}
}
((Tenant) person).getMessage(set);
}
}
}
抽象同事类
public abstract class Person {
protected String name;
protected Mediator mediator;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Mediator getMediator() {
return mediator;
}
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
public Person(String name, Mediator mediator) {
this.name = name;
this.mediator = mediator;
}
abstract void contact(String message);
}
房主
public class HouseOwner extends Person {
public HouseOwner(String name, Mediator mediator) {
super(name, mediator);
}
@Override
void contact(String message) {
System.out.println(name+"房主发布了一条消息:"+message);
mediator.contact(message,this);
}
public void getMessage(Set message){
System.out.println(name+"房主获得了消息:"+message);
}
}
public class Tenant extends Person{
public Tenant(String name, Mediator mediator) {
super(name, mediator);
}
@Override
void contact(String message) {
System.out.println(name+"购房者发布了一条消息:"+message);
mediator.contact(message,this);
}
void getMessage(Set message){
System.out.println(name+"购房者获得了消息:"+message);
}
}
测试
public class Client {
public static void main(String[] args) {
MediatorStructure mediatorStructure = new MediatorStructure();
HouseOwner houseOwnerOne = new HouseOwner("房主一号", mediatorStructure);
HouseOwner houseOwnerTwo = new HouseOwner("房主二号", mediatorStructure);
Tenant tenantOne = new Tenant("买房一号",mediatorStructure);
Tenant tenantTwo = new Tenant("买房一号",mediatorStructure);
mediatorStructure.setHouseOwners(houseOwnerOne);
mediatorStructure.setHouseOwners(houseOwnerTwo);
mediatorStructure.setTenants(tenantOne);
mediatorStructure.setTenants(tenantTwo);
tenantOne.contact("汤臣一品");
tenantOne.contact("汤臣二品");
tenantTwo.contact("汤臣三品");
tenantTwo.contact("汤臣四品");
houseOwnerOne.contact("汤臣一品");
houseOwnerTwo.contact("汤臣六品");
mediatorStructure.getMessage(houseOwnerOne);
mediatorStructure.getMessage(tenantOne);
}
}
买房一号购房者发布了一条消息:汤臣一品
买房一号购房者发布了一条消息:汤臣二品
买房一号购房者发布了一条消息:汤臣三品
买房一号购房者发布了一条消息:汤臣四品
房主一号房主发布了一条消息:汤臣一品
房主二号房主发布了一条消息:汤臣六品
房主一号房主获得了消息:[汤臣三品, 汤臣四品, 汤臣二品, 汤臣一品]
买房一号购房者获得了消息:[房主一号]
案例中存在两种同事角色,购房者以及房主,中介类保存不同同事类对象,然后根据不同同事类给出不同返回结果。
当同事类太多时,中介者的职责将很大,它会变得复杂而庞大,以至于系统难以维护。