This article mainly introduced the Python design pattern programming in the visitor and the observer pattern, the design pattern development facilitates the coordination of the team collaboration programming code, the need friend can refer to the next
Visitor mode
I think that the visitor mode is to add additional visitors to the code function to complete the extension of the existing program structure under the premise, why this use? When you have more classes, add new methods to a layer structure, if added or changed on the base class, may break the original design, there are compatibility problems, so only the required classes dynamically added.
Examples of Python
Here is an example of building a car, each part has an acceptance method to accept the so-called ' visitors ', and this visitor is in the form of parameters, but in fact, he is a class with some functions of the instance, it has a lot of visit beginning of the method corresponding to different parts. This makes it unnecessary to modify these parts, but only to modify the relevant parts of our visitor class.
# wheels, engine, body These definitions are no need to change class wheel:def init (self, name): Self.name = name def accept (self, visitor): # Each visitor is The same, but the method is not the same, such as Here is Visitwheel, # and then passed to self, think? He can do whatever he wants. Visitor.visitwheel (self) class engine:def accept (self, visitor): Visitor.visitengine (Self) class Body: def accept (self, visitor): Visitor.visitbody (self) # We're going to combine it into a car class car:def init (self): Self.engine = engine () se Lf.body = Body () Self.wheels = [Wheel ("front left"), Wheel (' Front right '), Wheel ("Back to Left"), Wheel ("BAC K right ")] # This does not need to be moving, he is just a combination of the above components, just do the properties of the delegate def accept (Self,visitor): Visitor.visitcar (self) self.engine.accept (vi Sitor) self.body.accept (visitor) for wheel in Self.wheels:wheel.accept (visitor) # This is our visitor, every time the change is in here. Class Pr Intvisitor:def Visitwheel (self, Wheel): print "visiting" +wheel.name+ "Wheel" def visitengine (self, Engine): Prin T "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 pattern
When we want the state of an object to change, then all of its objects can be changed (notified), then the observer pattern can be used, which is the object of the observer, and the object to be changed is the so-called ' observer '
Examples of Python
# This is the Observer base class Subject (object): Def init (self): Self._observers = [] # Add Dependent object def attach (self, observer): If no T observer in SELF._OBSERVERS:SELF._OBSERVERS.APPEND (Observer) # Cancel Add def detach (self, observer): Try:sel F._OBSERVERS.REMOVE (Observer) except Valueerror:pass # Here just notifies the above registered dependent objects of new changes to Def notify (self, modifier=none): For observer in Self._observers: # You can set the filter criteria for updates that do not meet the filter criteria if modifier! = Observer:observer.update (self) # view Reviewer class data (Subject): Def init (self, name= "): Super (data, self). init () Self.name = name Self._data = 0 # PYT hon2.6 adds a new notation, gets the property, sets the property to (assuming the property name is x) @x.setter, deletes the @x.deleter @property def data (self): return self._data @data. s Etter def data (self, value): Self._data = value self.notify () # There are 2 observers, that is, dependent objects, each time the data changes, the 2 view will change class Hexvie Wer (object): Def update (self, subject): print ' Hexviewer:subject%s has data 0x%x '% (Subject.name, subject.data) clas s Decimalviewer (object): def update (self, subject): print ' Decimalviewer:subject%s have data%d '% (subject.name, subject.data) if name = = ' Ma In ': data1 = data (' Data 1 ') Data2 = data (' Data 2 ') View1 = Decimalviewer () View2 = Hexviewer () Data1.attach (view1) d Ata1.attach (VIEW2) Data2.attach (view2) Data2.attach (view1) print "Setting Data 1 = ten" Data1.data = ten print "Setting Data 2 = "Data2.data =" Setting Data 1 = 3 "Data1.data = 3 print" Setting Data 2 = 5 "Data2.data = 5 pr int "Update data1 ' s view2 Because view1 is being filtered" data1.notify (modifier=view1) print "Detach hexviewer from Data1 and Data2. " Data1.detach (VIEW2) Data2.detach (view2) print "Setting data 1 = ten" Data1.data = ten print "Setting data 2 =" data2 . data = 15