Python版

https://github.com/faif/python-patterns/blob/master/behavioral/mediator.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- """
http://web.archive.org/web/20120309135549/http://dpip.testingperspective.com/?p=28 *TL;DR80
Encapsulates how a set of objects interact.
""" import random
import time class TC: def __init__(self):
self._tm = None
self._bProblem = 0 def setup(self):
print("Setting up the Test")
time.sleep(0.1)
self._tm.prepareReporting() def execute(self):
if not self._bProblem:
print("Executing the test")
time.sleep(0.1)
else:
print("Problem in setup. Test not executed.") def tearDown(self):
if not self._bProblem:
print("Tearing down")
time.sleep(0.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(0.1) def report(self):
print("Reporting the results of Test")
time.sleep(0.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(0.1)
# Following code is to simulate a communication from DB to TC
if random.randrange(1, 4) == 3:
return -1 def update(self):
print("Updating the test results in Database")
time.sleep(0.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()
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
for i in range(3):
tc = TC()
tc.setTM(tm)
tm.setTC(tc)
tc.setup()
tc.execute()
tc.tearDown() ### OUTPUT ###
# Setting up the Test
# Inserting the execution begin status in the Database
# Executing the test
# Tearing down
# Updating the test results in Database
# Reporting the results of Test
# Setting up the Test
# Inserting the execution begin status in the Database
# Reporter Class is preparing to report the results
# Problem in setup. Test not executed.
# Test not executed. No tear down required.
# Setting up the Test
# Inserting the execution begin status in the Database
# Executing the test
# Tearing down
# Updating the test results in Database
# Reporting the results of Test

Python转载版

【编程思想】【设计模式】【行为模式Behavioral】中介者模式Mediator的更多相关文章

  1. 设计模式的征途—22.中介者(Mediator)模式

    我们都用过QQ,它有两种聊天方式:一是私聊,二是群聊.使用QQ群,一个用户就可以向多个用户发送相同的信息和文件,从而无需一一发送,节省大量时间.通过引入群的机制,极大地减少系统中用户之间的两两通信,用 ...

  2. Java进阶篇设计模式之十 ---- 访问者模式和中介者模式

    前言 在上一篇中我们学习了行为型模式的解释器模式(Interpreter Pattern)和迭代器模式(Iterator Pattern).本篇则来学习下行为型模式的两个模式,访问者模式(Visito ...

  3. Java设计模式之十 ---- 访问者模式和中介者模式

    前言 2018年已经过去,新的一年工作已经开始,继续总结和学习Java设计模式. 在上一篇中我们学习了行为型模式的解释器模式(Interpreter Pattern)和迭代器模式(Iterator P ...

  4. C#设计模式之12:中介者模式

    中介者模式 在asp.net core中实现进程内的CQRS时用mediatR是非常方便的,定义command,然后定义commandhandler,或者notification和notificati ...

  5. 【设计模式 - 17】之中介者模式(Mediator)

    1      模式简介 中介者模式的定义: 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显式地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 中介者模式中的组成部分: 1. ...

  6. JS设计模式(11)中介者模式

    什么是中介者模式? 中介者模式:对象和对象之间借助第三方中介者进行通信. 定义:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的 ...

  7. 【设计模式】 模式PK:门面模式VS中介者模式

    1.概述 门面模式为复杂的子系统提供一个统一的访问界面,它定义的是一个高层接口,该接口使得子系统更加容易使用,避免外部模块深入到子系统内部而产生与子系统内部细节耦合的问题.中介者模式使用一个中介对象来 ...

  8. 设计模式-(9)中介者模式(swift)

    在对象去耦合的模式中,有两种模式:中介者模式,观察者模式 一,概念 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 这个 ...

  9. 轻松掌握:JavaScript代理模式、中介者模式

    代理模式.中介者模式 代理模式 在面向对象设计中,有一个单一职责原则,指就一个类(对象.函数)而言,应该仅有一个引起它变化的原因.如果一个对象承担了过多的职责,就意味着它将变得巨大,引起它变化的原因就 ...

  10. C#设计模式之十八中介者模式(Mediator Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第五个模式,该模式是[中介者模式],英文名称是:Mediator Pattern.还是老套路,先从名字上来看看.“中介者模式”我第一次看到这个名称,我的理解 ...

随机推荐

  1. 刷题日记-JZ25合并有序链表

    合并有序链表 递归方式合并链表pHead1,pHead2 base case是 pHead1为空或者pHead2为空 递归方式是 如果pHead1->val < pHead2->va ...

  2. 【.NET 与树莓派】用 MPD 制作数字音乐播放器

    树莓派的日常家居玩法多多,制作一台属于自己的数字音乐播放机是其中的一种.严格上说,树莓派是没有声卡的,其板载的 3.5 mm 音频孔实际是通过 PWM 来实现音频输出的(通过算法让PWM信号变成模拟信 ...

  3. charles抓包修改请求参数发送新的请求

    打开charles -->选择请求右击选择compose---修改参数发送请求

  4. Django笔记&教程 2-2 URL详细匹配规则

    Django 自学笔记兼学习教程第2章第2节--URL详细匹配规则 点击查看教程总目录 本章第一节中我们简单介绍了URL与View关系 简单概括来说,网页请求的url会通过urls.py里面的urlp ...

  5. 大爽Python入门教程 2-2 序列: 字符串、元组与列表

    大爽Python入门公开课教案 点击查看教程总目录 序列 序列(sequence): 顾名思义,有序的排列. 有序排列的一串数据. 一种容器,容器内成员有序排列. python的字符串str,元组tu ...

  6. jenkins bat删除指定路径下的文件及文件夹

    最近在用jenkins集成,生成allure测试报告,但是每次生成的allure测试报告,都是上一次执行的痕迹.比如这次我只运行了100个用例,结果显示运行2000条,上一次运行的用例,时间也涵括了上 ...

  7. Roslyn 编译器Api妙用:动态生成类并实现接口

    在上一篇文章中有讲到使用反射手写IL代码动态生成类并实现接口. 反射的妙用:C#通过反射动态生成类型继承接口并实现 有位网友推荐使用 Roslyn 去脚本化动态生成,今天这篇文章就主要讲怎么使用 Ro ...

  8. [nowcoder5669J]Jumping on the Graph

    考虑枚举$k$并求出$f(k)=\sum_{i=1}^{n}\limits\sum_{j=i+1}^{n}\limits [D(i,j)\le k]$,那么答案就是$\sum_{i=1}^{1e9}( ...

  9. [bzoj1107]驾驶考试

    转化题意,如果一个点k符合条件,当且仅当k能到达1和n考虑如果l和r($l<r$)符合条件,容易证明那么[l,r]的所有点都将会符合条件,因此答案是一个区间枚举答案区间[l,r],考虑如何判定答 ...

  10. 『学了就忘』Linux用户管理 — 51、用户管理相关命令

    目录 1.添加用户(useradd命令) 2.设定密码(passwd命令) 3.用户信息修改(usermod命令) 4.删除用户(userdel命令) 5.切换用户身份(su命令) 1.添加用户(us ...