【编程思想】【设计模式】【行为模式Behavioral】中介者模式Mediator
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的更多相关文章
- 设计模式的征途—22.中介者(Mediator)模式
我们都用过QQ,它有两种聊天方式:一是私聊,二是群聊.使用QQ群,一个用户就可以向多个用户发送相同的信息和文件,从而无需一一发送,节省大量时间.通过引入群的机制,极大地减少系统中用户之间的两两通信,用 ...
- Java进阶篇设计模式之十 ---- 访问者模式和中介者模式
前言 在上一篇中我们学习了行为型模式的解释器模式(Interpreter Pattern)和迭代器模式(Iterator Pattern).本篇则来学习下行为型模式的两个模式,访问者模式(Visito ...
- Java设计模式之十 ---- 访问者模式和中介者模式
前言 2018年已经过去,新的一年工作已经开始,继续总结和学习Java设计模式. 在上一篇中我们学习了行为型模式的解释器模式(Interpreter Pattern)和迭代器模式(Iterator P ...
- C#设计模式之12:中介者模式
中介者模式 在asp.net core中实现进程内的CQRS时用mediatR是非常方便的,定义command,然后定义commandhandler,或者notification和notificati ...
- 【设计模式 - 17】之中介者模式(Mediator)
1 模式简介 中介者模式的定义: 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显式地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 中介者模式中的组成部分: 1. ...
- JS设计模式(11)中介者模式
什么是中介者模式? 中介者模式:对象和对象之间借助第三方中介者进行通信. 定义:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的 ...
- 【设计模式】 模式PK:门面模式VS中介者模式
1.概述 门面模式为复杂的子系统提供一个统一的访问界面,它定义的是一个高层接口,该接口使得子系统更加容易使用,避免外部模块深入到子系统内部而产生与子系统内部细节耦合的问题.中介者模式使用一个中介对象来 ...
- 设计模式-(9)中介者模式(swift)
在对象去耦合的模式中,有两种模式:中介者模式,观察者模式 一,概念 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 这个 ...
- 轻松掌握:JavaScript代理模式、中介者模式
代理模式.中介者模式 代理模式 在面向对象设计中,有一个单一职责原则,指就一个类(对象.函数)而言,应该仅有一个引起它变化的原因.如果一个对象承担了过多的职责,就意味着它将变得巨大,引起它变化的原因就 ...
- C#设计模式之十八中介者模式(Mediator Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第五个模式,该模式是[中介者模式],英文名称是:Mediator Pattern.还是老套路,先从名字上来看看.“中介者模式”我第一次看到这个名称,我的理解 ...
随机推荐
- Linux mem 2.4 Buddy 内存管理机制
文章目录 1. Buddy 简介 2. Buddy 初始化 2.1 Struct Page 初始化 2.2 Buddy 初始化 3. 内存释放 4. 内存分配 4.1 gfp_mask 4.2 nod ...
- [JavaScript] 实现简单的表单数据校验功能
实现表单数据校验功能 因为项目用的UI库功能太少,表单不具备校验功能,所以自己写了一个,只有一个文件. 使用 import { required, email, useValidate } from ...
- 面向政务企业的开发者工具集-逐浪文本大师v0.1正式发布(含代码全部开源啦!)
这是一款基于.net 4.7环境开发的开发者工具. 一个实用的windows小工具集合,里面包含了多个常用的小软件.其中的批量修改文件名及文件内容功能,可以自定义修改规则,支持规则的导入与导出.不需要 ...
- 九. Go并发编程--context.Context
一. 序言 前几篇中提到 等待多个 goroutine 协作的方式可以使用WaitGroup. 但是有一种场景我们无论是使用Mutex, sync/Once,都无法满足. 场景如下 现在有一个 Ser ...
- 手把手教你学Dapr - 8. 绑定
目录 手把手教你学Dapr - 1. .Net开发者的大时代 手把手教你学Dapr - 2. 必须知道的概念 手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序 手把手教你学Dapr ...
- Linux系统查看磁盘可用空间的5个命令
大家好,我是良许. 工作中,经常会遇到磁盘爆满的情况,尤其是一台服务器运行了 N 年之后,里面会充满各种各样垃圾文件,比如:编译产生的中间文件.打包的镜像文件.日志文件,等等. 别问我怎么知道,我上家 ...
- [loj6736]最小连通块
定义$f(S)$表示点集$S$的最小连通块 做法1 通过对所有节点判定,可以在$n$次询问中求出具体的$f(S)$ 对于$x\ne y$,显然$(x,y)\in E$当且仅当$f(\{x,y\})=\ ...
- [luogu7599]雨林跳跃
为了方便,令$a_{0}=a_{n+1}=\infty$,另外$a_{i}$是两两不同的 记$L_{x}$和$R_{x}$分别为$x$左右两侧第一个比$a_{x}$大的元素位置,可以$o(n)$预处理 ...
- [luogu1390]公约数的和
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define N 2000005 4 long long n,ans,f[N],vi ...
- 监听器watch
<label > 姓名: <input type="text" placeholder="请输入姓名" v-model="firt ...