中介者模式
中介者模式是一种行为型模式,又叫调停者模式,它是为了办理多个对象之间,多个类之间通信的复杂性,定义一个中介者对象来封装一些列对象之间的交互,使各个对象之间不同持有对方的引用就可以实现交互,降低耦合度;实际开发中,消息队列、服务注册中心、MVC框架中的controller都是中介者;
图解
脚色
- 同事对象:定义抽象接口,用于与中介者举行通信,一般是一个发送消息的接口,一个接收消息的接口
- 详细同事对象:实现同事对象抽象接口,详细发送、接收消息的逻辑
- 中介者:定义要给抽象接口用于与同事对象举行通信
- 详细中介者:实现同事之间通信的逻辑
案例1-中介者持有同事对象的引用
中介者
- public interface Mediator {
- void execute();
- }
- /** 实现1*/
- public class AllPersonMediator implements Mediator {
- private List<Person> personList;
- public AllPersonMediator(List<Person> personList) {
- this.personList = personList;
- }
- @Override
- public void execute() {
- System.out.println("开家长会啦:");
- personList.forEach(item -> System.out.println(item.getName()));
- }
- }
- /** 实现2*/
- public class TeacherByTeacherMediator implements Mediator {
- private Person p1;
- private Person p2;
- public TeacherByTeacherMediator(Teacher p1, Teacher p2) {
- this.p1 = p1;
- this.p2 = p2;
- }
- @Override
- public void execute() {
- System.out.println(p1.getName()+"和" + p2.getName() + "两个老师之间的协同的任务");
- }
- }
- /** 实现3*/
- public class TeacherAndStudentAndParentsMediator implements Mediator {
- private Person p1;
- private Person p2;
- private Person p3;
- public TeacherAndStudentAndParentsMediator(Teacher p1, Student p2, Parents p3) {
- this.p1 = p1;
- this.p2 = p2;
- this.p3 = p3;
- }
- @Override
- public void execute() {
- System.out.println("老师"+p1.getName()+"通知家长"+p3.getName() +"学生" +p2.getName()+ "成绩不及格!");
- }
- }
复制代码 同事类:
- public abstract class Person {
- private String name;
- public Person(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- }
- public class Student extends Person {
- public Student(String name) {
- super(name);
- }
- }
- public class Student extends Person {
- public Student(String name) {
- super(name);
- }
- }
- public class Parents extends Person {
- public Parents(String name) {
- super(name);
- }
- }
复制代码 测试类:
- public class Test01 {
- public static void main(String[] args) {
- Teacher t1 = new Teacher("王老师");
- Teacher t2 = new Teacher("刘老师");
- Student s1 = new Student("小明");
- Parents p1 = new Parents("小明爸爸");
- TeacherByStudentMediator teacherByTeacherMediator = new TeacherByStudentMediator(t1, s1);
- teacherByTeacherMediator.execute();
- TeacherAndStudentAndParentsMediator teacherAndStudentAndParentsMediator = new TeacherAndStudentAndParentsMediator(t1, s1, p1);
- teacherAndStudentAndParentsMediator.execute();
- List<Person> list = new ArrayList<>();
- list.add(t1);
- list.add(t2);
- list.add(s1);
- list.add(p1);
- AllPersonMediator allPersonMediator = new AllPersonMediator(list);
- allPersonMediator.execute();
- }
- }
复制代码 案例2-同事类持有中介者的引用
中介者
- public interface SimpleMediator {
- Queue<Client> queue = new ConcurrentLinkedQueue<>();
- void add(Client c);
- Client get();
- }
- /** 实现*/
- public class MessageSimpleMediator implements SimpleMediator{
- @Override
- public void add(Client c) {
- queue.add(c);
- }
- @Override
- public Client get() {
- return queue.poll();
- }
- }
复制代码 同事类
- public abstract class Client<T> {
- protected String name;
- protected T message;
- protected SimpleMediator simpleMediator;
- public Client(String name, T message, SimpleMediator simpleMediator) {
- this.name = name;
- this.message = message;
- this.simpleMediator = simpleMediator;
- }
- public Client(String name, SimpleMediator simpleMediator) {
- this.name = name;
- this.simpleMediator = simpleMediator;
- }
- /** 发送消息*/
- abstract void sent();
- /** 获取消息*/
- abstract void get();
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public T getMessage() {
- return message;
- }
- public void setMessage(T message) {
- this.message = message;
- }
- }
- public class ReceiverClient extends Client<String>{
- public ReceiverClient(String name, SimpleMediator simpleMediator) {
- super(name, simpleMediator);
- }
- @Override
- void sent() {
- new RuntimeException("这是接收消息的客户端");
- }
- @Override
- void get() {
- Client client = simpleMediator.get();
- System.out.println("发送人:" + client.getName());
- System.out.println("发送内容:" + client.getMessage());
- }
- }
- public class SenderClient extends Client<String>{
- public SenderClient(String name, String message, SimpleMediator simpleMediator) {
- super(name, message, simpleMediator);
- }
- @Override
- void sent() {
- simpleMediator.add(this);
- System.out.println("消息发送成功!"); }
- @Override
- void get() {
- new RuntimeException("这是接收消息的客户端");
- }
- }
复制代码 测试类:
- public class Test02 {
- public static void main(String[] args) {
- MessageSimpleMediator messageSimpleMediator = new MessageSimpleMediator();
- SenderClient C1 = new SenderClient("发送消息的客户端1", "发送的内容", messageSimpleMediator);
- SenderClient C2 = new SenderClient("发送消息的客户端2", "发送的内容", messageSimpleMediator);
- SenderClient C3 = new SenderClient("发送消息的客户端3", "发送的内容", messageSimpleMediator);
- ReceiverClient R3 = new ReceiverClient("接收消息的客户端", messageSimpleMediator);
- C1.sent();
- C2.sent();
- C3.sent();
- while (messageSimpleMediator.queue.size() > 0){
- R3.get();
- }
- }
- }
复制代码 总结
在学习中介者模式的时候一直迷惑,为什么要有中介者接口?
学习完后感悟:计划模式是一种思想,不应该去套公式。中介者模式的核心思想就是通过对象引用的方式实现多个同事类通过一个中介者建立接洽,可以中介者中引用同事类,也可以同时类引用中介者,甚至可以用一个不用中介者接口,直接定义详细中介者实现。详细怎么实现要根据详细环境而论。
计划模式是一种思想,而不是一种公式模板。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |