-- 中介者Mediator模式,类之间的交互行为统一被放在了Mediator对象中,对象通过Mediator对象同其它对象交互
一、优缺点:
优点:
- 将关联对象集中在了中介者控制
- 类的重用性增加
- 将系统按功能分割成更小的对象,符合类的最小设计原则
缺点:
- 关系复杂,系统复杂性增加(很多设计模式的缺点,空间换扩展性)
二、模式结构
官方结构图 :
组成(角色) | 作用 |
---|---|
Colleague(同事) | 指一个种类,提供抽象方法 |
ConcreteColleague (具体同事) | 具体种类,实现抽象方法 |
Mediator(中介者) | 抽象中介者,提供抽象方法 |
ConcreteMediator (具体中介者) | 具体中介者,实现抽象方法 |
三、栗子 《游客-中介-房东》
-- 这里根据官方结构图进行创建
1、Person (指Colleague)
package com.behavior.mediator;
import lombok.Data;
/**
* @description: 抽象
* @author: ziHeng
* @create: 2018-08-11 15:41
**/
@Data//Data插件
public abstract class People {
//人名
private String name;
//可接受价格
private int acceptablePrice;
//中介
private Mediator mediator;
public People(String name, int acceptablePrice) {
this.name = name;
this.acceptablePrice = acceptablePrice;
}
//寻找中介
public abstract void findMediator(Mediator mediator);
}
(1)、Visitors 游客 (ConcreteColleague)
package com.behavior.mediator;
/**
* @description: 游客
* @author: ziHeng
* @create: 2018-08-11 15:51
**/
public class Visitors extends People{
public Visitors(String name, int acceptablePrice) {
super(name, acceptablePrice);
}
//找房子
public void findHouse(){
//注册信息
Mediator mediator = this.getMediator();
mediator.registerInfo(this);
//这里可以注册后寻求中介马上处理,这里就不弄了
//mediator.deal();
}
@Override
public void findMediator(Mediator mediator) {
this.setMediator(mediator);
}
}
(2)、Landord 房东 (ConcreteColleague)
package com.behavior.mediator;
/**
* @description: 房东
* @author: ziHeng
* @create: 2018-08-11 15:51
**/
public class Landlord extends People {
public Landlord(String name, int acceptablePrice) {
super(name, acceptablePrice);
}
//出租房子
public void rentOutHouse(){
//注册信息
Mediator mediator = this.getMediator();
mediator.registerInfo(this);
//这里可以注册后寻求中介马上处理,这里就不弄了
//mediator.deal();
}
@Override
public void findMediator(Mediator mediator) {
this.setMediator(mediator);
}
}
2、Mediator (中介抽象)
package com.behavior.mediator;
/**
* @description: 中介抽象类
* @author: ziHeng
* @create: 2018-08-11 15:41
**/
public interface Mediator {
//注册信息
void registerInfo(People people);
//中介处理
String deal();
}
(1)、租赁中介 (中介实现类)
package com.behavior.mediator;
import java.util.*;
/**
* @description: 租赁中介
* @author: ziHeng
* @create: 2018-08-11 15:53
**/
public class RentMediator implements Mediator {
//游客
private Map<Integer,Visitors> visitors;
//房东
private Map<Integer,Landlord> landlord;
public RentMediator() {
this.visitors = new HashMap<>();
this.landlord = new HashMap<>();
}
@Override
public void registerInfo(People people) {
int acceptPrice = people.getAcceptablePrice();
if(people instanceof Visitors){
visitors.put(acceptPrice,(Visitors) people);
}else if(people instanceof Landlord){
landlord.put(acceptPrice,(Landlord)people);
}
}
@Override
public String deal() {
if(visitors.isEmpty()){
return "中介处暂时没有游客资源";
}
if(landlord.isEmpty()){
return "中介处暂时没有房东资源";
}
//entrySet遍历效率更高
Iterator<Map.Entry<Integer, Landlord>> landlordEntrySet = landlord.entrySet().iterator();
Iterator<Map.Entry<Integer, Visitors>> visitorsEntrySet = visitors.entrySet().iterator();
while (landlordEntrySet.hasNext()){
Map.Entry<Integer, Landlord> landlordEntry = landlordEntrySet.next();
while(visitorsEntrySet.hasNext()){
Map.Entry<Integer, Visitors> visitorsEntry = visitorsEntrySet.next();
if(Objects.equals(landlordEntry.getKey(), visitorsEntry.getKey())){
return "匹配出合理的价格:"+landlordEntry.getKey()+" 游客"+visitorsEntry.getValue().getName()+"和"+landlordEntry.getValue().getName();
}
}
}
//keySet遍历
//for(Integer housePrice:landlord.keySet()){
// for(Integer acceptablePrice:visitors.keySet()){
// if(Objects.equals(housePrice, acceptablePrice)){
// return "匹配出合理的价格:"+acceptablePrice+" 姓名"+visitors.get(acceptablePrice).getName()+"-"+landlord.get(acceptablePrice).getName();
// }
// }
//}
return "没有合适的匹配选项";
}
}
调用Test类:
package com.behavior.mediator;
/**
* @description: 中介者模式测试
* @author: ziHeng
* @create: 2018-08-11 14:33
**/
public class MediatorTest {
public static void main(String[] args) {
//租赁中介
Mediator rentMediator = new RentMediator();
//游客
Visitors visitors = new Visitors("小明",300);
//寻找中介找房子
visitors.findMediator(rentMediator);
visitors.findHouse();
//房东
Landlord landlord = new Landlord("房东A",300);
//寻找中介出租房子
landlord.findMediator(rentMediator);
landlord.rentOutHouse();
//中介处理
String result = rentMediator.deal();
System.out.println("处理结果:"+result);
}
}