程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python design pattern - behavioral pattern - observer pattern

編輯:Python

Catalog

List of articles

  • Catalog
  • Observer mode
  • Application scenarios
  • Code example

Observer mode

Observer mode , Must contain “ The observer ” and “ Observed ” These two characters , And between the observer and the observed “ Observe ” Logical association of , When the observer changes , The observer will observe such changes , And respond accordingly . for example : Business data is the observed , The user interface is the observer .

actually , Observer mode is mostly a one to many relationship , Multiple observer objects can observe a certain observed object at the same time .

The realization idea of observer mode is : The core abstract class is used to manage all other classes that depend on it , When the core class changes , Proactively notify and update other classes .

Application scenarios

  • An abstract model has two aspects , One side depends on the other . Encapsulate these aspects in separate objects so that they can be changed and reused independently .
  • Changes to one object will cause changes to one or more other objects , I don't know how many objects will change , Can reduce the coupling between objects .
  • One object must notify other objects , I don't know who they are .
  • A trigger chain needs to be created in the system ,A The behavior of the object will affect B object ,B The behavior of the object will affect C object ,…. You can use the observer pattern to create a chain trigger mechanism .

Code example

When the number of customers decreases to the threshold , Sales will inform the factory to reduce production 、 At the same time, inform human resources to start layoffs , On the contrary, it increases .

class Observer:
""" Observer core class , Salesman """
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 number of 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 class , Personnel department """
def change(self, value):
if value < 10:
print(" Personnel changes : layoffs ")
elif value > 20:
print(" Personnel changes : Expansion of staff ")
else:
print(" Personnel will not be affected ")
class Factory:
""" Observer class , Factory """
def change(self, value):
if value < 15:
print(" Production plan changes : production ")
elif value > 25:
print(" Production plan changes : Increase production ")
else:
print(" The 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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved