• 28 行为型模式-中介者模式


    1 中介者模式介绍

    在这里插入图片描述
    在这里插入图片描述

    2 中介者模式原理

    在这里插入图片描述
    在这里插入图片描述

    3 中介者模式实现
    /**
     * 抽象中介者
     **/
    public interface Mediator {
    
        //处理同事对象注册与转发同事对象信息的方法
        void apply(String key);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    /**
     * 具体中介者
     **/
    public class MediatorImpl implements Mediator {
    
        @Override
        public void apply(String key) {
            System.out.println("最终中介者执行的操作,key为" + key);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    /**
     * 抽象同事类
     **/
    public abstract class Colleague {
    
        private Mediator mediator;
    
        public Colleague(Mediator mediator) {
            this.mediator = mediator;
        }
    
        public Mediator getMediator() {
            return mediator;
        }
    
        //同事间进行交互的抽象方法
        public abstract void exec(String key);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    /**
     * 具体同事类
     **/
    public class ConcreteColleagueA extends Colleague{
        public ConcreteColleagueA(Mediator mediator) {
            super(mediator);
        }
    
        @Override
        public void exec(String key) {
            System.out.println("=====在A同事中,通过中介者执行!");
            getMediator().apply(key);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    /**
     * 具体同事类
     **/
    public class ConcreteColleagueB extends Colleague{
        public ConcreteColleagueB(Mediator mediator) {
            super(mediator);
        }
    
        @Override
        public void exec(String key) {
            System.out.println("=====在B同事中,通过中介者执行!");
            getMediator().apply(key);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    public class Client {
    
        public static void main(String[] args) {
    
            //创建中介者
            Mediator mediator = new MediatorImpl();
    
            //创建同事对象
            Colleague c1 = new ConcreteColleagueA(mediator);
            c1.exec("key-A");
    
            Colleague c2 = new ConcreteColleagueB(mediator);
            c2.exec("key-B");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    4 中介者模式应用实例

    【例】租房

    /**
     * 抽象中介者
     **/
    public abstract class Mediator {
    
        //创建联络方法
        public abstract void contact(String message, Person person);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    /**
     * 抽象同事类
     **/
    public abstract class Person {
    
        protected String name;
    
        //持有中介者的引用
        protected Mediator mediator;
    
        public Person(String name, Mediator mediator) {
            this.name = name;
            this.mediator = mediator;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    /**
     * 具体的中介者-中介机构
     **/
    public class MediatorStructure extends Mediator {
    
        //中介知晓 房租出租人和承租人的信息
        private HouseOwner houseOwner;  //房主
    
        private Tenant tenant;  //租房者
    
        public Tenant getTenant() {
            return tenant;
        }
    
        public void setTenant(Tenant tenant) {
            this.tenant = tenant;
        }
    
        public HouseOwner getHouseOwner() {
            return houseOwner;
        }
    
        public void setHouseOwner(HouseOwner houseOwner) {
            this.houseOwner = houseOwner;
        }
    
        @Override
        public void contact(String message, Person person) {
            if(person == houseOwner){
                //如果是房主,则租房者获得信息
                tenant.getMessage(message);
            }else{
                //如果是租房者,则房租获得信息
                houseOwner.getMessage(message);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    /**
     * 具体同事类-房租拥有者
     **/
    public class HouseOwner extends Person {
    
        public HouseOwner(String name, Mediator mediator) {
            super(name, mediator);
        }
    
        //与中介联系
        public void contact(String message){
            mediator.contact(message,this);
        }
    
        //获取信息
        public void getMessage(String message){
            System.out.println("房主: " + name +",获取到的信息: " + message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    /**
     * 具体同事类-承租人
     **/
    public class Tenant extends Person {
    
        public Tenant(String name, Mediator mediator) {
            super(name, mediator);
        }
    
        public void contact(String message){
            mediator.contact(message,this);
        }
    
        public void getMessage(String message){
            System.out.println("租房者"+name+",获取到的信息: " + message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    public class Client {
    
        public static void main(String[] args) {
    
            //中介机构
            MediatorStructure mediator =  new MediatorStructure();
    
            //房主
            HouseOwner houseOwner = new HouseOwner("张三", mediator);
    
            //租房者
            Tenant tenant = new Tenant("李四", mediator);
    
            //中介收集房主及租房者信息
            mediator.setHouseOwner(houseOwner);
            mediator.setTenant(tenant);
    
            //租房人的需求
            tenant.contact("需要在天通苑附近找一个,两室一厅的房子一家人住,房租在5000~6000之间");
    
            //房主的需求
            houseOwner.contact("出租一套天通苑地跌站附近的两室一厅,房租6000,可谈");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    5 中介者模式总结

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    【leetcode刷题之路】面试经典150题(6)——图+图的广度优先搜索+字典树+回溯
    DJ12-2-2 算术运算指令
    【LeetCode:2558. 从数量最多的堆取走礼物 | 大根堆】
    无脑014——linux系统,制作coco(json)格式数据集,使用mmdetection训练自己的数据集
    测试覆盖率治不好你的精神内耗
    SSH协议&在IDEA中使用Git及git GUI的简单使用
    git中的cherry-pick和merge有些区别以及cherry-pick怎么用
    PostgreSQL教程(二十九):服务器管理(十一)之高可用、负载均衡和复制
    HTML中的语义化标签
    使用 CSS 伪类的attr() 展示 tooltip
  • 原文地址:https://blog.csdn.net/weixin_39563769/article/details/134084634