意图:

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

适用性:

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

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

想定制一个分布在多个类中的行为,而又不想生成太多的子类。

#!/usr/bin/python
#coding:utf8
'''
Mediator
'''
"""http://dpip.testingperspective.com/?p=28""" import time class TC:
def __init__(self):
self._tm = tm
self._bProblem = 0 def setup(self):
print("Setting up the Test")
time.sleep(1)
self._tm.prepareReporting() def execute(self):
if not self._bProblem:
print("Executing the test")
time.sleep(1)
else:
print("Problem in setup. Test not executed.") def tearDown(self):
if not self._bProblem:
print("Tearing down")
time.sleep(1)
self._tm.publishReport()
else:
print("Test not executed. No tear down required.") def setTM(self, TM):
self._tm = tm def setProblem(self, value):
self._bProblem = value class Reporter:
def __init__(self):
self._tm = None def prepare(self):
print("Reporter Class is preparing to report the results")
time.sleep(1) def report(self):
print("Reporting the results of Test")
time.sleep(1) def setTM(self, TM):
self._tm = tm class DB:
def __init__(self):
self._tm = None def insert(self):
print("Inserting the execution begin status in the Database")
time.sleep(1)
#Following code is to simulate a communication from DB to TC
import random
if random.randrange(1, 4) == 3:
return -1 def update(self):
print("Updating the test results in Database")
time.sleep(1) def setTM(self, TM):
self._tm = tm class TestManager:
def __init__(self):
self._reporter = None
self._db = None
self._tc = None def prepareReporting(self):
rvalue = self._db.insert()
if rvalue == -1:
self._tc.setProblem(1)
self._reporter.prepare() def setReporter(self, reporter):
self._reporter = reporter def setDB(self, db):
self._db = db def publishReport(self):
self._db.update()
rvalue = self._reporter.report() def setTC(self, tc):
self._tc = tc if __name__ == '__main__':
reporter = Reporter()
db = DB()
tm = TestManager()
tm.setReporter(reporter)
tm.setDB(db)
reporter.setTM(tm)
db.setTM(tm)
# For simplification we are looping on the same test.
# Practically, it could be about various unique test classes and their
# objects
while (True):
tc = TC()
tc.setTM(tm)
tm.setTC(tc)
tc.setup()
tc.execute()
tc.tearDown()

Mediator(中介者)的更多相关文章

  1. C++设计模式-Mediator中介者模式

    Mediator中介者模式作用:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. UML如下: Colleage抽象同事类 ...

  2. Mediator 中介者 协调者模式

    简介 定义:用一个[中介者对象]封装一系列的[对象交互],中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 中介者模式的结构 抽象中介者Mediator:定义好[ ...

  3. 设计模式 ( 十六 ): Mediator中介者模式 -- 行为型

    1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各个对象中. 对于一个模块或者系统,可能由很多对象构成,而且这些对象 ...

  4. 设计模式16:Mediator 中介者模式(行为型模式)

    Mediator 中介者模式(行为型模式) 依赖关系的转化 动机(Motivation) 在软件构建过程中,经常出现多个对象互相关联交互的情况,对象之间经常会维持一种复杂的应用关系,如果遇到一些需求的 ...

  5. Mediator(中介者)-对象行为型模式

    1.意图 用一个中介对象来封装一系列的对象交互.中介者使各个对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 2.动机 通过将集体行为封装在一个单独的中介者对象中,中介者 ...

  6. Mediator - 中介者模式

    定义 用一个中介对象来封装一系列的对象的交互.中介者使各对象不须要显示地相互使用,从而使其耦合松散,并且能够独立的改变他们之间的交互. 案例 比方有一个图像界面,在界面上有一个输入框LineEdit, ...

  7. 设计模式(17)--Mediator(中介者模式)行为型

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.模式定义: 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以 ...

  8. Mediator 中介者 MD

    中介者模式 简介 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 中介者模式也称为调解者模式或者调停者模式. 当程序存在大 ...

  9. 设计模式学习笔记——Mediator中介者模式

    将众多对象之间的网状关系转为全部通过一个中间对象间接发生关系,此中间对象为中介者. 看图最直观: 作用不言而喻,就是降低对象之间的耦合度,乃至降低了整个系统的复杂度. 有点象代理模式,更象外观模式:

  10. 设计模式之美:Mediator(中介者)

    索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Mediator 模式结构样式代码. 意图 用一个中介对象来封装一系列的对象交互. 中介者使各对象不需要显式地相互引用,从而使其 ...

随机推荐

  1. 微信小程序 --- 登录页面

    思路:在个人中心页面,首先判断全局的 app.js 里面的 globalData 里面的 is_login 状态,并且判断 缓存数据中的 is_login 状态,如果都为真,就正常显示,如果有一个为假 ...

  2. PHP概率算法---砸金蛋示例

    这是一个很经典的概率算法: function get_rand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr); ...

  3. Linux 安装 MantisBT 详解

    https://www.cnblogs.com/qdwyg2013/p/6030157.html 在工作中,选用了MantisBT作为公司的BTS工具.MantisBT的作为一款缺陷跟踪管理系统,有以 ...

  4. Gitlab安装和使用

     GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目.        GitLab拥有与Github类似 ...

  5. poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4422   Accepted: 1482 D ...

  6. apache代理weblogic集群办法

    方法一: --关闭iptables和selinux --在apache配置文件httpd.conf最下面添加如下语句,然后重启apache: ServerName 127.0.0.1:80 NameV ...

  7. lampp and testrail

    https://wyzx.testrail.net szllq2000 http://129.0.1.228/testrail/ http://docs.gurock.com/testrail-adm ...

  8. ES6基础教程(整理自阮一峰)

    ------------------------ECMAScript 6 简介------------------------ECMAScript 和 JavaScript 的关系是,前者是后者的规格 ...

  9. Windows:子线程中创建窗口

    一般来讲,UI的所有操作都必须在主线程,否则会出现未知错误.但有时候我们会需要一个功能比较单一的窗口,同时希望他在一个单独的线程运行.并不影响主线程的效率. 下面说明一下新建子线程创建的新窗口的方法, ...

  10. DNS的MX记录和CNAME记录(转)

    MX记录就是邮件域名对邮件服务器(域名)的映射.可以映射到多个邮件服务器,发送时会选择一台发送. 拿到新域名后,还要再查找DNS,将域名转不ip 原文:http://my.oschina.net/u/ ...