Home  >  Article  >  Backend Development  >  Python learning observer mode

Python learning observer mode

little bottle
little bottleforward
2019-04-28 10:41:101767browse

This article mainly talks about the observer mode of Python. The code has certain reference value. Interested friends can learn about it. I hope it can be helpful to you.

Demand: Employees are secretly looking at stocks at work. Please ask the front desk to notify them once the boss comes in and ask them to stop looking at stocks.

There are two types of people here, one is the observer, that is, the employee, and the other is the notifier, that is, the front desk. The employees are observing the status of the front desk, and the front desk is responsible for notifying employees of the latest developments.


#encoding=utf-8
__author__ = 'kevinlu1010@qq.com'

class Receptionist():
    def __init__(self):
        self.observes=[]
        self.status=''
    def attach(self,observe):
        self.observes.append(observe)
    def notify(self):
        for observe in self.observes:
            observe.update()

class StockObserve():
    def __init__(self,name,receptionist):
        self.name=name
        self.receptionist=receptionist
    def update(self):
        print '%s,%s停止看股票'%(self.receptionist.status,self.name)

if __name__=='__main__':
    receptionist=Receptionist()
    observe1=StockObserve('张三',receptionist)
    observe2=StockObserve('李四',receptionist)
    receptionist.attach(observe1)
    receptionist.attach(observe2)

    receptionist.status='老板来了'
    receptionist.notify()

The coupling between the two classes here is very large, and they are interdependent. On the one hand, the notify method of the foreground class will call the update method of the stock observer class. On the other hand, the observer class will access the status attribute of the foreground class to obtain the latest developments.

When the demand changes, for example, the boss can now be the notifier. In addition to watching stocks, employees will also watch NBA. If a Boss class and NBAObserver class are added, the coupling of these four classes will be very tight. , later maintenance will be very difficult, so when encountering such a tight coupling situation, it is necessary to abstract their coupled parts into a parent class, so that later maintenance will be much easier


#encoding=utf-8
__author__ = 'kevinlu1010@qq.com'
from abc import ABCMeta, abstractmethod

class Subject():
    __metaclass__ = ABCMeta
    observers=[]
    status=''
    @abstractmethod
    def attach(self,observer):
        pass
    @abstractmethod
    def detach(self,observer):
        pass
    @abstractmethod
    def notify(self):
        pass

class Observer():
    __metaclass__ = ABCMeta
    def __init__(self,name,sub):
        self.name=name
        self.sub=sub
    @abstractmethod
    def update(self):
        pass

class Boss(Subject):
    def __init__(self):
        pass
    def attach(self,observer):
        self.observers.append(observer)

    def detach(self,observer):
        self.observers.remove(observer)
    def notify(self):
        for observer in self.observers:
            observer.update()

class StockObserver(Observer):
    def update(self):
        print '%s,%s停止看股票'%(self.sub.status,self.name)
class NBAObserver(Observer):
    def update(self):
        print '%s,%s停止看NBA'%(self.sub.status,self.name)

if __name__=='__main__':
    boss=Boss()
    observe1=StockObserver('张三',boss)
    observe2=NBAObserver('李四',boss)
    boss.attach(observe1)
    boss.attach(observe2)
    boss.detach(observe2)
    boss.status='我是老板,我来了'
    boss.notify()

Related tutorials: Python video tutorial

The above is the detailed content of Python learning observer mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete