Requirements: Employees to work in the secretly look at the stock, please front desk once the boss came in, inform them, let them stop looking at the stock.
There are two types of people, one is the observer, that is, the staff, a kind of notice, that is, the front desk, the staff in the observation of the status of the front desk, the receptionist is responsible for informing employees of the latest developments.
#Encoding=utf-8__author__='[email protected]'classreceptionist ():def __init__(self): self.observes=[] Self.status="' defAttach (Self,observe): Self.observes.append (observe)defNotify (self): forObserveinchself.observes:observe.update ()classStockobserve ():def __init__(self,name,receptionist): Self.name=name Self.receptionist=ReceptionistdefUpdate (self):Print '%s,%s Stop looking at stocks'%(Self.receptionist.status,self.name)if __name__=='__main__': Receptionist=receptionist () observe1=stockobserve ('Zhang San', receptionist) Observe2=stockobserve ('John Doe', receptionist) Receptionist.attach (observe1) Receptionist.attach (observe2) receptionist.status='the boss is here.'receptionist.notify ()
The coupling of the two classes here is very large and they are interdependent. On the one hand, the Notify method of the foreground class invokes the Update method of the stock watcher class, and on the other hand, the Observer class accesses the status property of the calling foreground class to get the latest dynamic.
When the demand changes, such as now the boss can also be a notifier, employees in addition to watching stocks, but also watch the NBA, if you add a boss class and Nbaobserver class, so that the four classes of coupling will be very close, later maintenance will be very difficult, so when encountering this tight coupling situation, You need to abstract their coupled parts into a parent class so that later maintenance can be much easier
#Encoding=utf-8__author__='[email protected]' fromAbcImportAbcmeta, AbstractmethodclassSubject ():__metaclass__=Abcmeta observers=[]
Status= ' @abstractmethoddefAttach (self,observer):Pass@abstractmethoddefDetach (self,observer):Pass@abstractmethoddefNotify (self):PassclassObserver ():__metaclass__=Abcmetadef __init__(self,name,sub): Self.name=name Self.sub=Sub @abstractmethoddefUpdate (self):PassclassBoss (Subject):def __init__(self):Pass defAttach (Self,observer): Self.observers.append (Observer)defDetach (Self,observer): SELF.OBSERVERS.REMOVE (Observer)defNotify (self): forObserverinchself.observers:observer.update ()classStockobserver (Observer):defUpdate (self):Print '%s,%s Stop looking at stocks'%(Self.sub.status,self.name)classNbaobserver (Observer):defUpdate (self):Print '%s,%s Stop watching NBA'%(Self.sub.status,self.name)if __name__=='__main__': Boss=Boss () observe1=stockobserver ('Zhang San', boss) Observe2=nbaobserver ('John Doe', Boss) Boss.attach (observe1) Boss.attach (observe2) Boss.detach (observe2) boss.status='I'm the boss, I'm here.'boss.notify ()
Python design pattern-Observer pattern