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程序设计有所帮助。

相关文章

python多线程编程方式分析示例详解

在Python多线程中如何创建一个线程对象如果你要创建一个线程对象,很简单,只要你的类继承threading.Thread,然后在__init__里首先调用threading.Threa...

Python实现i人事自动打卡的示例代码

Python实现i人事自动打卡的示例代码

我司使用的打卡软件是 i 人事,不过我这记性,经常漏了打卡签退,定了闹钟都会忘,今天又被老大屌了。于是准备抓一下签到接口,利用 crontab 来实现自动签到签退。 环境配置 这里使用的...

python 性能提升的几种方法

关于python 性能提升的一些方案。 一、函数调用优化(空间跨度,避免访问内存)  程序的优化核心点在于尽量减少操作跨度,包括代码执行时间上的跨度以及内存中空间跨度。 1.大...

python nmap实现端口扫描器教程

python nmap实现端口扫描器教程

NMap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包。 nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端。确定哪些服务运行在哪些连接端...

基于Python和Scikit-Learn的机器学习探索

你好,%用户名%! 我叫Alex,我在机器学习和网络图分析(主要是理论)有所涉猎。我同时在为一家俄罗斯移动运营商开发大数据产品。这是我第一次在网上写文章,不喜勿喷。 现在,很多人想开...