Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the Observer pattern in Python language

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to implement the Observer pattern in Python language". In the daily operation, I believe that many people have doubts about how to implement the Observer pattern in Python language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to realize the Observer pattern in Python language"! Next, please follow the editor to study!

Sample code

The first is the defined observation object base class Observable base class. This class simply implements the observer login and de-login and notification methods. These methods are used in derived classes.

# observation object base class class Observable: def _ init__ (self): self.observers= [] def attach (self, observer): print (type (observer), 'has beenattachedclass') Self.observers.append (observer) def detach (self, observer): print (type (observer), 'has beendetachedstones') Self.observers.remove (observer) def notify (self): for oin self.observers: o.update ()

Next is the concrete observation object class. The responsibility of this class is to call the base class interface to notify the logged-in observer when its state changes.

# class ConcreteObservable (Observable): def _ init__ (self): Observable.__init__ (self) self.state= None

Def set_state (self, state): self.state= state self.notify ()

Def get_state (self): return self.state

The actual observer class only needs to implement the update interface that the base class of the observation object wants when notifying, as shown below:

# Observer representation class class ConcreteObserver (Observer): def _ init__ (self,observable): Observer.__init__ (self) self.observable= observable def update (self): print (type (self), self.observable.get_state ())

The following demonstration code registers the Observer object with the Observable object after building the representational class objects for Observable and Observer, respectively. When you change the state of the Observable object, you can get the output of the state change. If the observer changes the state of the Observable again after being deleted, the output of the state change cannot be obtained.

# main program if _ _ name__ ='_ main__': observable = ConcreteObservable () # build the observer observer= ConcreteObserver (observable) # the observer registers observable.attach (observer) # change the state of the observation object, generate the output observable.set_state ('State changed 1 observer') # the observer deletes the observable.detach (observer) # change the state of the observation object again No output observable.set_state ('State changed 2 changes') is generated

The output of the demo program is as follows:

Has been attached! Has been attached! State Changed1! State Changed1! Has been detached! State Changed2! At this point, the study on "how to implement the Observer pattern in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 259

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report