Python设计模式之中介模式简单示例

yipeiwu_com5年前Python基础

本文实例讲述了Python设计模式之中介模式。分享给大家供大家参考,具体如下:

Mediator Pattern:中介模式

中介模式提供了一系列统一的系统接口。此模式也被认为是行为模式,因为他能选择程序处理流程

当许多类开始在交互中产生结果时,可以选用中介模式。当软件开始组织的时候,许多用户的要求添加更多的功能。

这就导致了要和以前的类不断交互,除了新类。随着系统的复杂度加大,类之间的交互变得频繁,维护代码变得困难。

中介模式 就是为了解决这个问题,通过允许类之间的松耦合。这样中介模式就能了解系统中所有类的功能。类的功能就是与中介类进行交互。当类与类之间需要交互的时候,类就发送信息给中介,中介就转发信息给被请求的类。通过这样,类与类之间的复杂度就减少了。

一个简单的中介模式例子:

一个类型的中介模式例子可以在测试自动框架(包含4个类,TC,TestManager,Reporter ,DB)中被证明。

1.TC类是测试的响应,借助方法setup(),execute(),tearDown()。
2.Reporter类调用

当测试分类开始执行时,调用prepare方法。
当测试分类完成执行时,调用report()方法 ,
框架的测试响应就是好的帮助文档。

我也没弄懂中介模式,让人犯晕!

代码贴出来:

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)
    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)
  while(1):
    tc = TC()
    tc.setTM(tm)
    tm.setTC(tc)
    tc.setup()
    tc.execute()
    tc.tearDown()

运行结果:

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

Pycharm学习教程(7)虚拟机VM的配置教程

Pycharm学习教程(7)虚拟机VM的配置教程

设想这样一种情况,你在一个平台上操作你的工程,但你希望在另外一个平台上完善并运行它,这就是为什么Pycharm做了很多工作来支持远程调试。   在虚拟机上运行一个工程主要包含以下步骤:...

python实现月食效果实例代码

python实现月食效果实例代码

我们在学习Python当中的pygame模块时,我们都知道我们可以通过使用 pygame模块实现很多功能性的东西,但是很多人应该不知道怎么通过使用pygame实现月食的效果吧,接下来我就...

python实现异常信息堆栈输出到日志文件

将try except中捕获到的异常信息输出到日志文件中,方便查找错误原因,tranceback模块提供了把详细出错堆栈信息格式化成字符串返回函数format_exc(). 具体代码如下...

python实现NB-IoT模块远程控制

本来想尝试下如果不使用运营商网络应用平台情况下,只是在服务商服务器上是否可以实现对终端完全控制,如果这样可行,那么物联网应用服务端更有灵活性。实际情况下,很难实现和运营商网络对等的处理,...

Python对数据进行插值和下采样的方法

使用Python进行插值非常方便,可以直接使用scipy中的interpolate import numpy as np x1 = np.linspace(1, 4096, 1024...