Python design pattern-Observer pattern

Source: Internet
Author: User

Example 1:

Class service:    def __init__ (self, service_name, process_name,  Port, enable_monitor=none):        self.service_name =  service_name        self.process_name = process_name         self.port = port         self.mon = enable_monitor        self._process_status  = True        self._port_status = True      @property     def process_status (self):         return self._process_status     @process_status .setter     def process_status (Self, status):         self._process_status = status        self.mon.start_mon ()     @ Property    def port_status (self):         return self._port_status     @port_status. Setter    def port_ Status (self, status):         self._port_status = status         self.mon.start_mon () class action:     @classmethod     def send_sms_alarm (cls, content):         print ("sms alarm: {}". Format (content)      @classmethod      def send_email_alarm (cls, content):         print ("email alarm: {}". Format (content)) class monitor:    def __ Init__ (self):        self.services = []    def add_service ( Self, service):         self.services.append (Service)      def start_mon (self):         for ser in  self.services:            if not  ser.process_status:                 action.send_email_alarm ("service: {0} process: {1} status: {2}". Format (                      ser.service_name, ser.process_name, ser.process_status))              if not ser.port_status:                 action.send_email_alarm ("Service: {0} process: {1} status:  {2} ". Format (                     ser.service_name, ser.port, ser.port_status)) if __name__ ==   ' __main__ ':     mon = monitor ()     http =  Service ("http",  "httpd",  80, mon)     mysql = service ("MySQL",   "Mysqld",  3306, mon)     zabbix = service ("Zabbix",  "Zabbixd ",  1501, mon)     mon.add_service (HTTP)     mon.add_service ( MySQL)     mon.add_service (Zabbix)      #mon. Start_mon ()      http.port_status = false


Example 2:

#coding: Utf-8#inventory class describes the warehouse object class inventory:    def __init__ (self):         self.observers = []  #此列表用于存储观察者对象          self._product = None  #产品          self._quantity = 0  #数量     def attach (self, observer):   #此方法用于将观察者对象添加进列表         self.observers.append (Observer)       @property   #使用property装饰器修饰 to turn the method into a property     def product (self):         return self._product     @product. Setter   #使用setter修饰product属性使其可以设置值     def product (self, value):         self._product = value         Self._update_observers ()   #只要设置了产品的值, call the  _update_observers method      @property   #对数量的设置      Def quantity (self):        return self._quantity      @quantity. setter    def quantity (Self, value):         self._quantity = value         Self._update_observers ()   #只要设置了数量的值, call the  _update_observers method     def _update_ Observers (self):         for observer in self.observers:   #遍历观察者对象             observer ()   #直接用 () The Observer object can be called directly because the __call__ method is implemented in the Consoleobserver class #consoleobserver class describes the Observer object class consoleobserver:     def __init__ (self, inventory):         self.inventory = inventory    def __call__ (self):  #实现__call__方法后可直接可调用, the job here is to print the product and quantity information         print ("Product: {0}, quantity: {1}". Format ( self.inventory.product, self.inventory.quantity)) if __name__ ==  ' __main__ ':     i = inventory ()   #创建仓库对象     c = consoleobserver (i)   #创建一个观察者对象, and the warehouse object as the initialization parameter     i.attach (c)   #将观察者对象添加到仓库对象中的observers列表里 (interaction between two objects)     i.product =  widgets   #仓库对象设置产品名称 (with changes)      i.quantity = 5  #仓库对象设置产品数量 (changes)      "     Workflow Analysis:         1, first added a product name, so notify the viewer to print, at this time the newly added product name is printed, the number defaults to 0         2, after the increase in the number of 5, then notify the Observer, at this time to print out the previously added product name and this time a new increase in the number of 5     ' ''


Python design pattern-Observer pattern

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.