Learn some Python basics-data structures, algorithms, design patterns---visitor patterns

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.