设计模式-行为型模式,python访问者模式

时间:2023-03-08 18:08:26
设计模式-行为型模式,python访问者模式

访问者模式

在访问者模式(Visitor Pattern)中,我们使用了一个访问者类,它改变了元素类的执行算法。通过这种方式,元素的执行算法可以随着访问者改变而改变。这种类型的设计模式属于行为型模式。根据模式,元素对象已接受访问者对象,这样访问者对象就可以处理元素对象上的操作。

介绍

意图:主要将数据结构与数据操作分离。

主要解决:稳定的数据结构和易变的操作耦合问题。

何时使用:需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免让这些操作"污染"这些对象的类,使用访问者模式将这些封装到类中。

如何解决:在被访问的类里面加一个对外提供接待访问者的接口。

关键代码:在数据基础类里面有一个方法接受访问者,将自身引用传入访问者。

应用实例:您在朋友家做客,您是访问者,朋友接受您的访问,您通过朋友的描述,然后对朋友的描述做出一个判断,这就是访问者模式。

优点: 1、符合单一职责原则。 2、优秀的扩展性。 3、灵活性。

缺点: 1、具体元素对访问者公布细节,违反了迪米特原则。 2、具体元素变更比较困难。 3、违反了依赖倒置原则,依赖了具体类,没有依赖抽象。

使用场景: 1、对象结构中对象对应的类很少改变,但经常需要在此对象结构上定义新的操作。 2、需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免让这些操作"污染"这些对象的类,也不希望在增加新操作时修改这些类。

注意事项:访问者可以对功能进行统一,可以做报表、UI、拦截器与过滤器。

#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):
pass class 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())
return if __name__ == '__main__':
clientUI();