中介者保护一组用户的交互,用户之间通过中介者进行交互,而不是间接交互。
中介者模式种个别有以下角色
- 形象中介者:定义中介者须要的执行办法。
具体中介者:实现用户交互的具体逻辑。
用户类:引入中介者,向中介者输入须要交互的内容。
上面通过一个婚介核心的实例来实现中介者模式
形象中介者 – 婚介核心
public interface DatingAgency {
/* **
退出会员 *
@param dog*
/
void join(SingleDog dog);
/* **
依据年龄范畴选会员 *
@param min*
@param max*
@return*
/
List<SingleDog> getList(Integer min, Integer max, SingleDog.SexEnum sexEnum);
}
具体中介者 – 王婆婚介所
public class WangPoDatingAgency implements DatingAgency{private List<SingleDog> singleDogs = new ArrayList<>();
@Override
public void join(SingleDog dog) {System.out.println("欢送"+dog.getName()+"退出王婆婚介所");
singleDogs.add(dog);
}
@Override
public List<SingleDog> getList(Integer min, Integer max, SingleDog.SexEnum sexEnum) {return singleDogs.stream().filter(dog -> {if (dog.getAge() >= min && dog.getAge() <= max && !sexEnum.equals(dog.getSex())) return true;
return false;
}).collect(Collectors.toList());
}
}
形象用户 – 独身人员
public abstract class SingleDog {
DatingAgency agency;
private String name;
private Integer age;
private SexEnum sex;
public enum SexEnum{MAN("男"),WOMAN("女");
private String sex;
SexEnum(String sex) {this.sex = sex;}
}
public SingleDog(DatingAgency agency, String name, Integer age, SexEnum sex) {
this.agency = agency;
this.name = name;
this.age = age;
this.sex = sex;
agency.join(this);
}
/* **
依据年龄筛选同性 *
@return*
/
public abstract List<SingleDog> pickByAge(Integer min,Integer max);
public abstract void introduce();
public DatingAgency getAgency() {return agency;}
public void setAgency(DatingAgency agency) {this.agency = agency;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public Integer getAge() {return age;}
public void setAge(Integer age) {this.age = age;}
public SexEnum getSex() {return sex;}
}
具体用户 – 程序员用户
public class Programmer extends SingleDog{public Programmer(DatingAgency agency, String name, Integer age, SexEnum sex) {super(agency, name, age, sex);
}
@Override
public List<SingleDog> pickByAge(Integer min, Integer max) {return agency.getList(min,max,getSex());
}
@Override
public void introduce() {System.out.println("读以下代码能够获取我的 QQ 号");
System.out.println("0110 1001 0101 0001 0010 0011 0100 ......");
}
}
具体用户 – 恋爱经验丰富的独身人员
public class ExperienceSingleDog extends SingleDog{public ExperienceSingleDog(DatingAgency agency, String name, Integer age, SexEnum sex) {super(agency, name, age, sex);
}
@Override
public List<SingleDog> pickByAge(Integer min, Integer max) {return agency.getList(min,max,getSex());
}
@Override
public void introduce() {System.out.println("你是我的心你是我的肝你是我的四分之三!");
}
}
测试
public class MediatorTest {
@Test
public void test(){DatingAgency agency = new WangPoDatingAgency();
SingleDog dog1 = new Programmer(agency,"程序员 A",23, SingleDog.SexEnum.MAN);
SingleDog dog2 = new Programmer(agency,"程序员 B",29, SingleDog.SexEnum.MAN);
SingleDog dog3 = new Programmer(agency,"程序员 C",35, SingleDog.SexEnum.WOMAN);
SingleDog dog4 = new ExperienceSingleDog(agency,"渣男 A",24, SingleDog.SexEnum.MAN);
SingleDog dog5 = new ExperienceSingleDog(agency,"渣女 A",31, SingleDog.SexEnum.WOMAN);
List<SingleDog> singleDogs = dog5.pickByAge(24, 35);
for (SingleDog dog:singleDogs) {System.out.println("====================");
System.out.println("名字"+dog.getName());
System.out.println("年龄"+dog.getAge());
dog.introduce();}
}
}
====== 后果 ======
欢送程序员 A 退出王婆婚介所
欢送程序员 B 退出王婆婚介所
欢送程序员 C 退出王婆婚介所
欢送渣男 A 退出王婆婚介所
欢送渣女 A 退出王婆婚介所
====================
名字程序员 B
年龄 29
读以下代码能够获取我的 QQ 号
0110 1001 0101 0001 0010 0011 0100 ......
====================
名字渣男 A
年龄 24
你是我的心你是我的肝你是我的四分之三!
长处
升高了类的复杂度,将本来用户之间的一对多,变成了用户和中介之间的一对一
将用户之间解耦
毛病
中介者会十分宏大,难以保护