博客
关于我
设计模式-中介者模式
阅读量:771 次
发布时间:2019-03-23

本文共 3704 字,大约阅读时间需要 12 分钟。

设计模式-中介者模式(Mediator Pattern)

问题引出

在智能家居系统中,各个设备(如闹钟、咖啡机、窗帘、电视等)通过通信协调彼此的工作状态。然而,直接的通信方式会导致设备间耦合度高,维护性差,且扩展性不足。这种复杂的耦合关系使得系统难以扩展和维护。我们需要找到一种更高效的方式来处理这些设备间的通信。

传统解决方法

在传统的智能家居系统中,各个设备通过直接调用彼此的方法来实现状态变化。这意味着:

  • 每个设备都需要明确知道其他设备的接口
  • 状态传递可能变得混乱
  • 增加新设备或改变流程会导致大量的重写工作

然而,这样的设计存在以下问题:

  • 当各设备状态多变时,相互调用会过于复杂
  • 设备间高度耦合,难以松解耦合
  • 消息传递容易混乱
  • 增加新设备或改变流程会降低系统的可维护性和扩展性

中介者模式

中介者模式通过引入一个中间对象来封装各个对象之间的交互。这样,各对象无需直接引用对方,减少耦合程度,提高系统的维护性和扩展性。

中介者模式的原理

中介者模式是一个行为模式,其核心思想是将对象间的交互封装在一个中介者对象中。具体表现为:

  • 各对象通过中介者对象进行通信
  • 中介者对象管理各对象之间的关系
  • 中介者模式降低了对象间的耦合度,提高了系统的可维护性和扩展性

中介者模式的实现示例

考虑智能家居系统的设备间通信,使用中介者模式可以通过以下方式实现:

同事接口

package com.atguigu.mediator.smarthouse;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(int stateChange);}

具体设备实现

各设备实现Colleague接口,通过中介者对象进行通信:

public class Alarm extends Colleague {    public Alarm(Mediator mediator, String name) {        super(mediator, name);        mediator.Register(name, this);    }    public void SendAlarm(int stateChange) {        SendMessage(stateChange);    }    public void SendMessage(int stateChange) {        this.GetMediator().GetMessage(stateChange, this.name);    }}

中介者实现

中介者类定义了设备间的关系,并协调消息传递:

public class ConcreteMediator extends Mediator {    private HashMap
colleagueMap; private HashMap
interMap; public ConcreteMediator() { colleagueMap = new HashMap<>(); interMap = new HashMap<>(); } public void Register(String colleagueName, Colleague colleague) { colleagueMap.put(colleagueName, colleague); if (colleague instanceof Alarm) { interMap.put("Alarm", colleagueName); } else if (colleague instanceof CoffeeMachine) { interMap.put("CoffeeMachine", colleagueName); } else if (colleague instanceof TV) { interMap.put("TV", colleagueName); } else if (colleague instanceof Curtains) { interMap.put("Curtains", colleagueName); } } public void GetMessage(int stateChange, String colleagueName) { if (colleagueMap.get(colleagueName) instanceof Alarm) { if (stateChange == 0) { String coffeeMachineName = interMap.get("CoffeeMachine"); String tvName = interMap.get("TV"); ((CoffeeMachine) colleagueMap.get(coffeeMachineName)).StartCoffee(); ((TV) colleagueMap.get(tvName)).StartTv(); } else if (stateChange == 1) { String tvName = interMap.get("TV"); ((TV) colleagueMap.get(tvName)).StopTv(); } } else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) { String curtainsName = interMap.get("Curtains"); ((Curtains) colleagueMap.get(curtainsName)).UpCurtains(); } }}

测试用例

客户端测试中介者模式的实现:

public class ClientTest {    public static void main(String[] args) {        Mediator mediator = new ConcreteMediator();        Alarm alarm = new Alarm(mediator, "alarm");        CoffeeMachine coffeeMachine = new CoffeeMachine(mediator, "coffeeMachine");        Curtains curtains = new Curtains(mediator, "curtains");        TV tv = new TV(mediator, "TV");        alarm.SendAlarm(0);        coffeeMachine.FinishCoffee();        alarm.SendAlarm(1);    }}

中介者模式的注意事项和细节

  • 多个类相互耦合:使用中介者模式可以将网状耦合结构转化为星型结构,减少耦合度。
  • 减少依赖性:符合迪米特原则,减少类间依赖。
  • 责任集中:中介者承担了多个对象之间的通信责任,一旦中介者出现问题,整个系统会受到影响。
  • 设计复杂性:中介者对象的设计需要特别注意,避免自身复杂化。

总结

中介者模式通过引入中介者对象将设备间的耦合度降低,提高了系统的可维护性和扩展性。在智能家居系统中,中介者模式非常适合处理多个设备间的状态协调问题。通过正确设计中介者类和相互关系,可以实现松耦合的高效通信,提升系统整体性能和用户体验。

转载地址:http://fitzk.baihongyu.com/

你可能感兴趣的文章
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>