An example is provided to illustrate the visitor and observer modes in Python Design Mode Programming.

Source: Internet
Author: User
This article mainly introduces the visitor and observer modes in the Python Design Mode Programming. The development of the design mode is conducive to the coordination of the team collaborative programming code. If you need it, you can refer Visitor Mode
I think the Visitor mode is used to complete code function expansion by adding additional visitors while modifying the existing program structure. Why? When you have many class layers and add new methods to a layer structure, adding or changing them to the base class may damage the original design and have compatibility problems, therefore, you only need to dynamically Add the required class.

Python example
Here is an example of building a car. Each component has an accept method to accept what I said above as 'access', which is passed in as a parameter, however, it is actually an instance of a class that contains some functions. It has many methods starting with visit that correspond to different components. In this way, you do not need to modify these components, but only the relevant parts of our visitor class.

# The definition of the Wheel, engine, and body does not need to be changed. class Wheel: def _ init _ (self, name): self. name = name def accept (self, visitor): # every visitor is the same, but the methods are different. For example, visitWheel is used here. # self is passed in. Think about it? In fact, visitor can do whatever he wants. visitWheel (self) class Engine: def accept (self, visitor): visitor. visitEngine (self) class Body: def accept (self, visitor): visitor. visitBody (self) # We need to combine into a Car class Car: def _ init _ (self): self. engine = Engine () self. body = Body () self. wheels = [Wheel ("front left"), Wheel ("front right"), Wheel ("back left"), Wheel ("back right")] # This does not need to be moved. It is just a combination of the components above, but a delegate def accept (self, visitor) for the attribute: visitor. visitCar (self) self. engine. accept (visitor) self. body. accept (visitor) for wheel in self. wheels: wheel. accept (visitor) # This is our visitor. Every modification is made here. class PrintVisitor: def visitWheel (self, wheel): print "Visiting" + wheel. name + "wheel" def visitEngine (self, engine): print "Visiting engine" def visitBody (self, body): print "Visiting body" def visitCar (self, car ): print "Visiting car" if _ name _ = '_ main _': car = Car () visitor = PrintVisitor () car. accept (visitor)


Observer Mode
When we want the state of an object to change, the dependency changes accordingly with all its objects (for notification), so we can use the Observer mode, these dependent objects are the object of the observer, and the object to be changed is the so-called 'observer'

Python example

# This is the observer base class Subject (object): def _ init _ (self): self. _ observers = [] # Add dependent object def attach (self, observer): if not observer in self. _ observers: self. _ observers. append (observer) # cancel def detach (self, observer): try: self. _ observers. remove (observer) failed t ValueError: pass # Here, the newly registered dependency object is notified of the new change def Policy (self, modifier = None): for observer in self. _ observers: # You can set filtering conditions to update if modifier that does not meet the filtering conditions.! = Observer: observer. update (self) # observer class Data (Subject): def _ init _ (self, name = ''): super (Data, self ). _ init _ () self. name = name self. _ data = 0 # New Method for python2.6. Set the property to property (assuming the property name is x) @ x. setter, delete as @ x. deleter @ property def data (self): return self. _ data @ data. setter def data (self, value): self. _ data = value self. notify () # Here there are two observer objects, that is, dependent objects. Each time Data is changed, these two views change class HexViewer (object): def update (self, subject): print 'hexviewer: Subject % s has data 0x % x' % (subject. name, subject. data) class DecimalViewer (object): def update (self, subject): print 'decimalviewer: Subject % s has data % d' % (subject. name, subject. data) if _ name _ = '_ main _': data1 = Data ('data 1') data2 = Data ('data 2 ') view1 = DecimalViewer () view2 = HexViewer () data1.attach (view1) data1.attach (view2) data2.attach (view2) data2.attach (view1) print "Setting Data 1 = 10" data1.data = 10 print "Setting Data 2 = 15" data2.data = 15 print "Setting Data 1 = 3" data1.data = 3 print "Setting Data 2 = 5 "data2.data = 5 print" Update data1's view2 Because view1 is be filtered "data1.notify (modifier = view1) print "Detach HexViewer from data1 and data2." data1.detach (view2) data2.detach (view2) print "Setting Data 1 = 10" data1.data = 10 print "Setting Data 2 = 15" data2.data = 15

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.