In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of the behavioral observer mode in Python, which is very detailed and has certain reference value. Friends who are interested must finish it!
1. Observer model
The observer model must include the roles of "observer" and "observed", and there is a logical relationship between the observer and the observed. when the observed changes, the observer will observe such changes and respond accordingly. For example, business data is the observer, and the user interface is the observer.
In fact, the observer model is mostly an one-to-many relationship, in which multiple observer objects can observe a certain object at the same time.
The implementation idea of the observer pattern is that the core abstract class is used to manage all other classes that depend on it, and actively notify and update other classes when the core class changes.
Second, application scenarios
An abstract model has two aspects, one of which depends on the other. Encapsulate these aspects in separate objects so that they can be changed and reused independently.
The change of one object will lead to the change of one or more other objects, but we don't know how many objects will change, which can reduce the coupling between objects.
An object must notify other objects without knowing who those objects are.
You need to create a trigger chain in the system. The behavior of An object will affect B object, and the behavior of B object will affect C object. You can use observer mode to create a chained trigger mechanism.
Third, code example
When the number of customers is reduced to a threshold, sales will inform the factory to reduce production and human resources to start layoffs, and vice versa.
Class Observer: observer core class Salesperson "" def _ init__ (self): self._number = None self._department = [] @ property def number (self): return self._number @ number.setter def number (self) Value): self._number = value print ('current customers: {}' .format (self._number)) for obj in self._department: obj.change (value) print ('-') def notice (self) Department): "relevant departments" self._department.append (department) class Hr: "Observer category" Personnel Department def change (self, value): if value
< 10: print("人事变动:裁员") elif value >20: print ("personnel change: expansion") else: print ("personnel is not affected") class Factory: "" Observer class, factory class "def change (self, value): if value
< 15: print("生产计划变动:减产") elif value >25: print ("production plan change: increase production") else: print ("production plan remains unchanged") if _ _ name__ = ='_ main__': observer = Observer () hr = Hr () factory = Factory () observer.notice (hr) observer.notice (factory) observer.number = 10 observer.number = 15 observer.number = 20 observer Number = 25 and above are all the contents of the article "sample Analysis of Behavioral Observer patterns in Python" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.