【设计模式】—— 中介者模式Mediator
前言:【模式总览】——————————by xingoo
模式意图
使用一个中介的对象,封装一组对象之间的交互,这样这些对象就可以不用彼此耦合。
这个中介者常常起着中间桥梁的作用,使其他的对象可以利用中介者完成某些行为活动,因此它必须对所有的参与活动的对象了如指掌!
应用场景
1 当一组对象要进行沟通或者业务上的交互,但是其关系却又很复杂混乱时,可以采用此模式。
2 当一个对象与其他的对象要进行紧密的交互,但又想服用该对象而不依赖其他的对象时。
3 想创造一个运行于多个类之间的对象,又不想生成新的子类时。
模式结构

Mediator 抽象的中介者,定义中介的规范
interface Mediator{
public void colleagueChanged(Colleague c);
}
ConcreteMediator 具体的中介者,通常内部依赖于多个业务对象
class ConcreteMediator implements Mediator{
private Colleague1 col1;
private Colleague2 col2;
public void colleagueChanged(Colleague c) {
col1.action();
col2.action();
}
public void createConcreteMediator() {
col1 = new Colleague1(this);
col2 = new Colleague2(this);
}
private Colleague1 getCol1() {
return col1;
}
public Colleague2 getCol2() {
return col2;
}
}
Colleague 抽象的业务角色
abstract class Colleague{
private Mediator mediator;
public Colleague(Mediator mediator){
this.mediator = mediator;
}
public Mediator getMediator() {
return mediator;
}
public abstract void action();
public void change(){
mediator.colleagueChanged(this);
}
}
Colleague1 Colleague2 具体的业务角色
class Colleague1 extends Colleague{
public Colleague1(Mediator m){
super(m);
}
public void action(){
System.out.println("this is an action from Colleague1");
}
}
class Colleague2 extends Colleague{
public Colleague2(Mediator m){
super(m);
}
public void action(){
System.out.println("this is an action from Colleague2");
}
}
全部代码
package com.xingoo.test.design.mediator;
abstract class Colleague{
private Mediator mediator; public Colleague(Mediator mediator){
this.mediator = mediator;
} public Mediator getMediator() {
return mediator;
} public abstract void action(); public void change(){
mediator.colleagueChanged(this);
}
}
class Colleague1 extends Colleague{
public Colleague1(Mediator m){
super(m);
}
public void action(){
System.out.println("this is an action from Colleague1");
}
}
class Colleague2 extends Colleague{
public Colleague2(Mediator m){
super(m);
}
public void action(){
System.out.println("this is an action from Colleague2");
}
}
interface Mediator{
public void colleagueChanged(Colleague c);
}
class ConcreteMediator implements Mediator{
private Colleague1 col1;
private Colleague2 col2; public void colleagueChanged(Colleague c) {
col1.action();
col2.action();
} public void createConcreteMediator() {
col1 = new Colleague1(this);
col2 = new Colleague2(this);
} private Colleague1 getCol1() {
return col1;
} public Colleague2 getCol2() {
return col2;
} } public class Client {
public static void main(String[] args) {
ConcreteMediator mediator = new ConcreteMediator();
mediator.createConcreteMediator();
Colleague1 col1 = new Colleague1(mediator);
// Colleague2 col2 = new Colleague2(mediator);
mediator.colleagueChanged(col1);
}
}
运行结果
this is an action from Colleague1
this is an action from Colleague2
生活中的设计模式

毕业的同学们,第一个要解决的问题就是租房子,当白富美高富帅出没社会后,穷屌丝没了生存之地。但是只要勤劳,一样有饭吃有房住!
这里房屋中介好比是一个中介者,它知道每个租客的身份信息,当有房屋出租后,它会发送给每一个租客消息。
这样,租客们中有一个变化活动时,都会利用房屋中介,发送消息到其他的租客。下面就是模仿的一个过程。
房屋中介代码如下:
interface StateMediator{
public void sell(Tenant tenant);
}
class RealEstateAgents implements StateMediator{
private TenantA teA;
private TenantB teB;
private TenantC teC;
public void sell(Tenant tenant) {
System.out.println("海景洋房 已经租出去了!");
if(tenant instanceof TenantA){
teB.crying();
teC.crying();
}else if(tenant instanceof TenantB){
teA.crying();
teC.crying();
}else if(tenant instanceof TenantC){
teB.crying();
teA.crying();
}
}
public void createAgents(){
teA = new TenantA(this);
teB = new TenantB(this);
teC = new TenantC(this);
}
}
租客的代码如下:
abstract class Tenant{
private RealEstateAgents agent;
public Tenant(RealEstateAgents agent) {
this.agent = agent;
}
public abstract void crying();
public void renting(){
agent.sell(this);
}
}
class TenantA extends Tenant{
public TenantA(RealEstateAgents agent) {
super(agent);
}
public void crying() {
System.out.println("我是高富帅 TenantA!哎呀我想要!");
}
}
class TenantB extends Tenant{
public TenantB(RealEstateAgents agent) {
super(agent);
}
public void crying() {
System.out.println("我是白富美 TenantB!哎呀我想要!");
}
}
class TenantC extends Tenant{
public TenantC(RealEstateAgents agent) {
super(agent);
}
public void crying() {
System.out.println("我是穷屌丝 TenantC!哎呀我想要!");
}
}
产生的业务活动如下:
public class ClientTest {
public static void main(String[] args) {
RealEstateAgents agent = new RealEstateAgents();
agent.createAgents();
System.out.println("TeA 抢到了房子了!");
agent.sell(new TenantA(agent));
System.out.println("过了两个月 TeB 抢到了房子了!");
agent.sell(new TenantB(agent));
}
}
运行结果
TeA 抢到了房子了!
海景洋房 已经租出去了!
我是白富美 TenantB!哎呀我想要!
我是穷屌丝 TenantC!哎呀我想要!
过了两个月 TeB 抢到了房子了!
海景洋房 已经租出去了!
我是高富帅 TenantA!哎呀我想要!
我是穷屌丝 TenantC!哎呀我想要!
【设计模式】—— 中介者模式Mediator的更多相关文章
- 设计模式-中介者模式(Mediator)
场景分析: 众所周知,电脑有很多组成部分,如硬盘.内存.光驱.音频.键盘等,各个组件之间协同工作才能保证电脑的正常运行. 如果各个组件之间直接交互,可能会比较复杂,如下图: 将上面的各个组件抽象成类, ...
- 大话设计模式--中介者模式 Mediator --C++实现实例
1. 中介者模式: 用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立改变他们之间的交互. Mediator的出现减少了各个Colleague的耦 ...
- 23种设计模式--中介者模式-Mediator Pattern
一.中介者模式的介绍 中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再 ...
- 深入浅出设计模式——中介者模式(Mediator Pattern)
模式动机 在用户与用户直接聊天的设计方案中,用户对象之间存在很强的关联性,将导致系统出现如下问题: 系统结构复杂:对象之间存在大量的相互关联和调用,若有一个对象发生变化,则需要跟踪和该对象关联的其他 ...
- 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...
- 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 二十四种设计模式:中介者模式(Mediator Pattern)
中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...
- JAVA 设计模式 中介者模式
用途 中介者模式 (Mediator) 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式是一种行为型模式. 结 ...
随机推荐
- js阻止事件冒泡的两种方法
1.什么是JS事件冒泡 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这 ...
- C# 含转义符的字符串处理
如果一个字符串中含有特殊字符,比如"号,如何将一个含有引号"的字符串赋值给一个变量. string a = @"sfsfsf"""; str ...
- jquery获取具有多个类class的标签内容
var tag = $('div.firstClassName.secondClassName.thirdClassName'); 注意空格
- 02-Maven安装配置
1.Maven下载 2.Maven依赖 3.安装Maven 4.Maven目录
- 20155325 Exp4 恶意代码分析
系统运行监控 (1)使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间并分析该文件,综述一下分析结果.目标就是找出所有连网的程序,连了哪里,大约干了什么(不抓 ...
- UI自动化环境准备
1.安装专业版pycharm,只要是填写了licensed的都是专业版本的pycharm 2.python中安装好selenium包 pip install selenium 3.python中安装 ...
- BugkuCTF web2
前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...
- Python3 str去除空格
一.去除str两端空格(strip()) a.去除左端空格 lstrip() str0='abcdef' str1=' abcdef' print(str0) print(str1.lstrip() ...
- shell实现压缩多个文件
Linux环境下写一个脚本 从键盘让用户输入几个文件,脚本能够将此几个文件归档压缩成一个文件: 1.首先介绍一下case语句格式 case SWITCH in value1) statement .. ...
- lambda表达式,map函数
lambda只是一个表达式,不需要定义函数,故也是匿名函数,用法为:lambda 参数:表达式. x=5 list1=[2,3,4] list2=[10,20,30] s=lambda x:x**3 ...