23种设计模式学习记录之中介者模式

本文深入探讨设计模式在软件工程中的核心作用,强调其对代码维护性、通用性和扩展性的提升,详细解析中介者模式,通过具体实例展示如何在实际业务中应用设计模式,提升软件开发效率。

想说的话:
在大学的时候曾经学过这23种设计模式,但是那时没啥编程经验,糊里糊涂过了一遍,没多久就忘记了,工作之后将精力主要集中在学习新技术上,比如springboot,cloud,docker,vue。毕业两年后去面试,发现设计模式还是java程序员需要迈过的一道坎,面试的时候问到代理模式和适配器模式有什么区别,你在工作中用到了什么设计模式,怎么用的?答不上来的特别尴尬,所以决定重新学习这几种设计模式,争取在工作中使用上。

本文所有案例代码

码云:https://gitee.com/helloworld6379/designPattern
Github:Github地址

设计模式概述

1 设计模式是程序员在面对同类软件工程设计问题所总结出来的有用的经验,模式不是代码,而是某类问题的通
用解决方案,设计模式(Design pattern)代表了最佳的实践。这些解决方案是众多软件开发人员经过相当长的
一段时间的试验和错误总结出来的。
2 设计模式的本质提高 软件的维护性,通用性和扩展性,并降低软件的复杂度。
3 设计模式并不局限于某种语言,java,php,c++ 都有设计模式.

设计模式类型
设计模式分为三种类型,共 23 种
1 创建型模式:单例模式、抽象工厂模式、原型模式、建造者模式、工厂模式。
2 结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。
3 行为型模式:模版方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter 模式)、状态模式、策略模式、职责链模式(责任链模式)。

简单介绍

顾名思义,这个模式的作用就跟现实中的中介一样,
房东和租户不再互相找来找去,房东把房源给中介,租户直接找中介。
在这里插入图片描述
中介者模式主要由这四个角色组成, 抽象中介者(Mediator)、具体中介者(ConcreteMediator)、 抽象同事类(Colleague)和具体同事类(ConcreteColleague) 。

抽象中介者(Mediator): 定义了同事对象到中介者对象之间的接口。
具体中介者(ConcreteMediator): 实现抽象中介者的方法,它需要知道所有的具体同事类,同时需要从具体的同事类那里接收信息,并且向具体的同事类发送信息。
抽象同事类(Colleague): 定义了中介者对象的接口,它只知道中介者而不知道其他的同事对象。
具体同事类(ConcreteColleague) : 每个具体同事类都只需要知道自己的行为即可,但是他们都需要认识中介者。

代码实现:

23种模式学到现在,发现网上很多文章,举例子的时候都是使用形象化的例子来举例,
比如适配器模式都是使用插座那个举例,感觉这样虽然能帮助理解,
但是在开发过程中遇到了可以使用设计模式的场景,并不能马上想起使用设计模式解决问题,
所以我感觉举例子的时候尽量使用实际业务来举例。

经常讲的MVC框架就是中介者模式,其中C(控制器)就是 M(模型)和 V(视图)的中介者。
但是百度了一天,MVC模式只能M改变的时候,调用C改变V,V貌似不能调用C。
遂放弃,想了一下,貌似实际业务中并没有相似的案例,日了狗了。

还是用实际生活中的例子:
房东通过中介发布房子信息,租客向中介发送想要的房子

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

package com.struggle.pattern.mediator;

/**
 * @Description 中介者抽象类
 * @Author LiuXing
 * @Date 2020/06/13 22:04
 */
public abstract class Mediator {

    //接收消息,向外部发布
    public abstract void getMessage(String address, String model,String colleagueName);

    public abstract void register(String colleagueName, Colleague colleague);
}

package com.struggle.pattern.mediator;

import java.util.HashMap;

/**
 * @Description 具体的中介
 * @Author LiuXing
 * @Date 2020/06/13 23:14
 */
public class ConcreteMediator extends Mediator {

    //集合,放入所有的房东和租户
    private HashMap<String, Colleague> colleagueMap;

    public ConcreteMediator() {
        colleagueMap = new HashMap<>();
    }

    @Override
    public void getMessage(String address, String model, String colleagueName) {
        Colleague colleague = colleagueMap.get(colleagueName);
        if (colleague instanceof Landlord){
            System.out.println(colleagueName +"发布了一间" + address + "的" + model);
        }else{
            System.out.println(colleagueName +"想要一间" + address + "的" + model);
        }

    }

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);
    }
}
package com.struggle.pattern.mediator;

/**
 * @Description 房东和租客抽象类
 * @Author LiuXing
 * @Date 2020/06/13 22:20
 */
public abstract class Colleague {

    private Mediator mediator;
    public String name;

    public Colleague(Mediator mediator, String name) {
        this.mediator = mediator;
        this.name = name;
    }

    //获取中介
    public Mediator getMediator() {
        return this.mediator;
    }

    //发布房子信息
    public abstract void sendMessage(String address, String model);
}
package com.struggle.pattern.mediator;

/**
 * @Description 房东
 * @Author LiuXing
 * @Date 2020/06/14 0:14
 */
public class Landlord extends Colleague {

    public Landlord(Mediator mediator, String name) {
        super(mediator, name);
        mediator.register(name,this);
    }

    //接收消息,向外部发布
    @Override
    public void sendMessage(String address, String model) {
        this.getMediator().getMessage(address,model,this.name);
    }

    public void publishHouseInfo(String address, String model){
        sendMessage(address,model);
    }
}
package com.struggle.pattern.mediator;

/**
 * @Description 租户
 * @Author LiuXing
 * @Date 2020/06/13 23:21
 */
public class Renter extends Colleague {

    public Renter(Mediator mediator, String name) {
        super(mediator, name);
        mediator.register(name,this);
    }

    @Override
    public void sendMessage(String address, String model) {
        this.getMediator().getMessage(address,model,this.name);
    }

    public void wantHouseInfo(String address, String model){
        sendMessage(address,model);
    }
  
}
package com.struggle.pattern.mediator;

/**
 * @Description
 * @Author LiuXing
 * @Date 2020/06/14 0:17
 */
public class MediatorTest {

    public static void main(String[] args) {

        Mediator mediator = new ConcreteMediator();
        Landlord landlordA = new Landlord(mediator, "房东A");
        Landlord landlordB = new Landlord(mediator, "房东B");
        Renter renterA = new Renter(mediator, "租客A");
        Renter renterB = new Renter(mediator, "租客B");

        landlordA.publishHouseInfo("昌平区天露园一区18-4-302","西边次卧");
        landlordB.publishHouseInfo("昌平区天露园二区16-4-302","南边主卧");

        renterA.wantHouseInfo("昌平区龙泽街道附近","次卧");
        renterB.wantHouseInfo("昌平区西二旗附近","主卧");

    }
}
房东A发布了一间昌平区天露园一区18-4-302的西边次卧
房东B发布了一间昌平区天露园二区16-4-302的南边主卧
租客A想要一间昌平区龙泽街道附近的次卧
租客B想要一间昌平区西二旗附近的主卧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值