中介者模式就是利用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地互相引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。

就像租房的中介系统,房主跟租房者不需要知道彼此只需要,只要在中介系统发布消息。

如此房主跟租房者之间不需要建立复杂关系,他们都只需要跟中介建立关系。

通常在以下情况下考虑使用中介者模式:

(1)系统中对象之间存在复杂的引用关系,产生的相互依赖关系结构混乱且难以理解。

(2)一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。

(3)一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。

(4)想通过一个中间类来封装多各类中的行为,而又不想生成太多的子类,可以通过引入中介者类来实现,在中介者中定义多个对象交互的公共行为,如果改变行为则可以增加新的中介者类。

(5)欲使一个后端数据模型能够被多个前端用户界面连接

抽象中介者:

 public abstract class Meditor {
abstract void Send(String message,Person person);
abstract void SetRenter(Person renter);
abstract void SetLandlord(Person landlord);
}

抽象同事类:

 public abstract class Person {
Meditor m_Meditor;// person 内部需要有一个中介者
abstract void SetMeditor(Meditor meditor);// 为 meditor 初始化
abstract void SendMessage(String message);//发送租房信息
abstract void GetMessage(String message);//获取租房信息
}

具体中介者:

 public class HouseMeditor extends Meditor {
private Person renter,landlord;
@Override
void Send(String message, Person person) {
if (person.equals(renter)) {
landlord.GetMessage(message);
}else {
renter.GetMessage(message);
}
} @Override
void SetRenter(Person renter) {
this.renter = renter;
} @Override
void SetLandlord(Person landlord) {
this.landlord = landlord;
} }

具体同事类:

房主:

 public class Landlord extends Person {

     @Override
void SetMeditor(Meditor meditor) {
m_Meditor = meditor;
} @Override
void SendMessage(String message) {
m_Meditor.Send(message, this);
} @Override
void GetMessage(String message) {
System.out.println("房东收到信息:" + message);
} }

租房者:

 public class Renter extends Person{

     @Override
void GetMessage(String message) {
System.out.println("租房者收到信息:" + message);
} @Override
void SetMeditor(Meditor meditor) {
m_Meditor = meditor;
} @Override
void SendMessage(String message) {
m_Meditor.Send(message, this);
} }

客户测试类:

 public class MeditorPattern {
public static void main(String[] args) { Meditor meditor = new HouseMeditor(); Person renter = new Renter();
Person landlord = new Landlord(); meditor.SetRenter(renter);
meditor.SetLandlord(landlord); renter.SetMeditor(meditor);
landlord.SetMeditor(meditor); renter.SendMessage("我想租套房子!");
landlord.SendMessage("我要出租房子!");
}
}

运行结果:

房东收到信息:我想租套房子!
租房者收到信息:我要出租房子!

中介者模式(Mediator、ConcreteMediator、Colleague Class)(租房中介)的更多相关文章

  1. 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  2. 二十四种设计模式:中介者模式(Mediator Pattern)

    中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...

  3. 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...

  4. 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...

  5. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...

  6. 设计模式 笔记 中介者模式 Mediator

    //---------------------------15/04/27---------------------------- //Mediator 中介者模式----对象行为型模式 /* 1:意 ...

  7. 设计模式之中介者模式(Mediator)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程.它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  8. 中介者模式(Mediator Pattern)

    用于减少多个对象或类之间的通信复杂性. 此模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来维护代码.中介者模式属于行为模式类别. 实现实例 在这里通过一个聊天室的示例来演示中 ...

  9. 23种设计模式--中介者模式-Mediator Pattern

    一.中介者模式的介绍     中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再 ...

随机推荐

  1. 创建vue项目及引入插件

    部署开发环境 安装淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org 安装webpack cnpm install ...

  2. fastjson json转linkedhashmap为null

    试了几种JSONObject.parseObject的方法,返回的都是null: 使用Gson就可以转成功. LinkedHashMap<String, String> map = gso ...

  3. 杂项-公司:Axway

    ylbtech-杂项-公司:Axway Axway 公司是法国Sopra 集团从事应用系统集成(EAI/B2Bi)软件及相关咨询服务业务的全资子公司.Axway公司成立于1980年,总部位于美国凤凰城 ...

  4. PHP实现图片的汉明码提取与降维

    作者感言:数学不好,遇到算法问题分分钟狗带,毫无转寰的余地-_-||| 最近心血来潮,看了相似图片的搜索,最最最初级的方法即提取汉明码,之后匹配汉明距离.当然,在数以亿计的汉明码中,要筛出需要的图片, ...

  5. C++ 连接上期所CTP交易行情接口

    CTP相关接口和文档下载: http://www.simnow.com.cn/static/softwareDownload.action 相关库文件以及头文件如下: 遇到的问题: 1.运行直接退出了 ...

  6. 网页多媒体 flash

    网页上的视频一般都是Flash格式的,因为Flash的兼容性比较好,再一个Flash文件的压缩以后文件较小. 提示:Flash动画的文件扩展名:.swf 以Flash动画为例,播放Flash动画的代码 ...

  7. day 38 MySQL之单表查询

    MySQL之单表查询   阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER ...

  8. PAT甲级——A1087 All Roads Lead to Rome【30】

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  9. re 模块 (正则的使用)

    一.正则表达式 英文全称: Regular Expression. 简称 regex或者re.正则表达式是对字符串操作的一种逻辑公式. 我们一般使用正则表达式对字符串进行匹配和过滤. 使用正则的优缺点 ...

  10. MyBatis与JPA的区别

    参考博客: https://www.cnblogs.com/llywy/p/10103136.html https://www.jianshu.com/p/32ce87c163d6 MyBatis分为 ...