The process of implementing the observer mode has many forms. The more intuitive one is to use the form of "registration-Notification-revoke registration. The following three figures describe such a process in detail: 1. The Observer registers himself as a Subject, the observed object stores the observer in a Container. 2. The observed object has a certain change (SomeChange in), obtains all registered observers from the container, and notifies the observer of the change. 3. The unobserved observer tells the observer to undo the observer and remove the observer from the container. When an observer registers himself to the container of the observer, the observer should not ask the specific type of the observer, but should use the observer interface. This advantage is: if there are other observers in the program, as long as the observer is also the same interface implementation. One observer can correspond to multiple observers. When the observer changes, he can notify all the observers one by one. Based on interfaces rather than specific implementations-This provides more flexibility for programs. # Coding: utf8 class Observer (object): "Observer class" def _ init _ (self, subject): # Set a topic self. subject = subject # Add this observer self to this topic. subject. addObserver (self) # The Observer holds the data self. fansnum = self. subject. fansnum def update (self, fansnum): self. fansnum = fansnum self. display () def display (self): print self. fansnum class Subject (object): "" theme class "def _ init _ (self): # observer list self. observerList = [] self. fansnum = 0 # Add the observer def addObserver (self, observer): self. observerList. append (observer) # Delete the observer def deleteObserver (self, observer): if observer in self. observerList: self. observerList. remove (observer) # notify the observer to update def policyobservers (self): for observer in self. observerList: observer. update (self. fansnum) # notify the observer when the value changes def setValue (self, fansnum): self. fansnum = fansnum # notify the observer self. policyobservers () if _ name _ = '_ main _': subject = Subject () observer = Observer (subject) observer. display () subject. setValue (2) subject. setValue (3)