侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   402

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

Mediator Pattern:中介模式

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

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

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

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

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

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

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

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

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

代码贴出来:

import timeclass 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 = valueclass 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 = tmclass 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 = 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()    rvalue = 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)  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+opencv实现动态物体追踪
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客