To tell the truth, the feeling is not very much, may not encounter too many scenes,
So there's no sense in the application scenario.
Anyway, all kinds of patterns are the instances of the class passed, over time, produced some rules ... :)
#The wheels, the engines, the bodies, the definitions, they don't have to change.classWheel:def __init__(self, name): Self.name=namedefAccept (self, visitor):#each visitor is the same, but the method is different, for example, here is Visitwheel, #and then passed in self, think about it? He can do whatever he wants.Visitor.visitwheel (self)classEngine:defAccept (self, visitor): Visitor.visitengine (self)classBody:defAccept (self, visitor): Visitor.visitbody (self)#we're going to combine them into cars .classCar:def __init__(self): Self.engine=Engine () self.body=Body () self.wheels= [Wheel ("Front Left"), Wheel ("Front Right"), Wheel (" back to left"), Wheel (" back Right") ] #this does not need to be moving, he is just a combination of the above components, just do the property of the delegate defAccept (Self,visitor): Visitor.visitcar (self) self.engine.accept (visitor) self.body.accept (Visito R) forWheelinchself.wheels:wheel.accept (visitor)#This is our visitor, and every change is here .classPrintvisitor:defVisitwheel (self, Wheel):Print "Visiting"+wheel.name+"Wheel" defVisitengine (self, Engine):Print "Visiting engine" defvisitbody (self, body):Print "Visiting Body" defVisitcar (self, car):Print "Visiting Car"if __name__=='__main__': Car=Car () visitor=printvisitor () car.accept (visitor)
Learn some Python basics-data structures, algorithms, design patterns---visitor patterns