zoukankan      html  css  js  c++  java
  • 【编程思想】【设计模式】【行为模式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转载版
  • 相关阅读:
    8.请描述基本数据类型和引用数据类型的区别?
    7.在第4题中Hello.class所在路径下, 输入命令:java Hello.class,会出现什么结果,为什么?
    6.如果第4题中在DOS命令下输入:java Hello 出现以下结果:Exception in thread “main” java.lang.NoClassDefFoundError: Hello
    5.如果第4题中在DOS命令下输入:java Hello 出现以下结果:Bad command or the file name 可能是什么原因?请说明理由。
    C# 反射技术应用
    C# 中的委托和事件
    类和结构的区别
    c#接口和抽象类的区别
    Repeater 控件使用总结
    SpringMvc+jquery easyui模块开发7步骤
  • 原文地址:https://www.cnblogs.com/demonzk/p/9035625.html
Copyright © 2011-2022 走看看