访问者模式(python)
来源:互联网 发布:小猪cms生活通o2o系统 编辑:程序博客网 时间:2024/06/06 01:35
访问者模式:表示一个作用于某对象结构中的各元素的操作。它可以使你在不改变各元素的类的前提下定义作用于这些元素的新操作。
类图:
访问者模式适用于数据结构相对稳定而基于该数据结构的操作需要经常扩展的系统。因为该模式的优点就是增加新的操作很容易。
#encoding=utf-8##by panda#访问模式def printInfo(info): print unicode(info, 'utf-8').encode('gbk')#基本数据结构:class Person(): def Accept(self, visitor): pass class Man(Person): type = '男人' def Accept(self, visitor): visitor.GetManConclusion(self)class Woman(Person): type = '女人' def Accept(self, visitor): visitor.GetWomanConclusion(self)#基于数据结构的操作class Action(): def GetManConclusion(self, person): pass def GetWomanConclusion(self, person): passclass Success(Action): type = '成功' def GetManConclusion(self, person): printInfo('%s %s时,背后多半有一个伟大的女人' %(person.type, self.type)) def GetWomanConclusion(self, person): printInfo('%s %s时,背后大多有一个不成功的男人' %(person.type, self.type))class Failing(Action): type = '失败' def GetManConclusion(self, person): printInfo('%s %s时,闷头喝酒,谁也不用劝' %(person.type, self.type)) def GetWomanConclusion(self, person): printInfo('%s %s时,眼泪汪汪,谁也劝不了' %(person.type, self.type))class Love(Action): type = '恋爱' def GetManConclusion(self, person): printInfo('%s %s时,凡是不懂也要装懂' %(person.type, self.type)) def GetWomanConclusion(self, person): printInfo('%s %s时,遇事懂也装作不懂' %(person.type, self.type))#对象结构类:遍历数据结构的操作class ObjectStructure: elements = [] def Attach(self, element): self.elements.append(element) def Detach(self, element): self.elements.remove(element) def Display(self, visitor): for e in self.elements: e.Accept(visitor) def clientUI(): o = ObjectStructure() o.Attach(Man()) o.Attach(Woman()) o.Display(Success()) o.Display(Failing()) o.Display(Love()) returnif __name__ == '__main__': clientUI();
类图:
- 访问者模式(python)
- 访问者模式(python)
- python设计模式(访问者模式)
- python设计模式之访问者模式
- Python设计模式(十五)【访问者模式】
- 设计模式-访问者模式-visitor-python
- python设计模式之访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 访问者模式
- 关于Internet Explorer 6(IE6)不支持CSS中!important属性
- 黑马程序员_关于object类的学习
- linux(fedora 15) 下常用软件安装
- BlackBerry NFC API target.getUri(Target.ISO_14443_3)返回的结果
- s3c2440启动过程详解
- 访问者模式(python)
- 布隆过滤
- 查看环境变量
- 2440init.s分析
- 关于使用BJX-LINK-001调试stm8s系列主控问题
- 跌倒老人讹人时等于自掘坟墓
- 关于Java中的paint,repaint,update三个方法的关系
- 如何调试android源码
- How to remove Exchange 2007 from a computer