博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【编程思想】【设计模式】【行为模式Behavioral】中介者模式Mediator
阅读量:5166 次
发布时间:2019-06-13

本文共 3359 字,大约阅读时间需要 11 分钟。

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;DR80Encapsulates how a set of objects interact."""import randomimport timeclass 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 = valueclass 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 = tmclass 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 = tmclass 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 = tcif __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转载版

 

转载于:https://www.cnblogs.com/demonzk/p/9035625.html

你可能感兴趣的文章
[Linux]文件浏览
查看>>
获取国内随机IP的函数
查看>>
今天第一次写博客
查看>>
江城子·己亥年戊辰月丁丑日话凄凉
查看>>
Spring Mvc模式下Jquery Ajax 与后台交互操作
查看>>
(转)matlab练习程序(HOG方向梯度直方图)
查看>>
『Raid 平面最近点对』
查看>>
【ADO.NET基础-数据加密】第一篇(加密解密篇)
查看>>
STL中的优先级队列priority_queue
查看>>
UE4 使用UGM制作血条
查看>>
浏览器对属性兼容性支持力度查询网址
查看>>
虚拟机长时间不关造成的问题
查看>>
面试整理:Python基础
查看>>
Program exited with code **** 相关解释
查看>>
tableView
查看>>
Happy Great BG-卡精度
查看>>
Xamarin Visual Studio不识别JDK路径
查看>>
菜鸟“抄程序”之道
查看>>
Ubuntu下关闭防火墙
查看>>
TCP/IP 邮件的原理
查看>>