Observer mode for design patterns (Python implementation) __python

Source: Internet
Author: User

Here is a second design pattern in learning the simple design pattern: Observer mode

Define the Observer pattern first: The Observer pattern defines a one-to-many dependency between objects, so that when an object changes state, all its objects are dependent and automatically updated.

For a specific example, look at the following figure:

The observer pattern is used quite extensively in practical applications. This design pattern embodies the loose coupling mechanism between the subject object and the Observer object, the Subject object has a state, and whenever the state changes, it notifies the observer registered in its queue in turn. But the main object does not know exactly what the observer is, it simply invokes an interface left by an observer. The advantage of this design is that many objects are prevented from accessing the same data at the same time, this is actually a push way, whether to add new observers or to reduce the observer, we only need to register and logoff, do not need to change the main object of the core code, with great flexibility.

This embodies a design principle:

strive for loose coupling between interactive objects.

The book mainly cites an example of the application of meteorological monitoring. The application of the observer model can be a good solution to this problem. Let's look at the Designed class diagram first.

The final code implemented in Python is as follows:

"' The second design pattern:observer pattern KeyNote: ' Class observer:def update (self, temp, humidity , pressure): return def display (self): return class Subject:def registerobserver (self, observer) : Return def removeobserver (self, observer): Return DEF notifyobservers (self): return CLA SS Weatherdata (Subject): def __init__ (self): Self.observers = [] self.temperature = 0.0 self.h umidity = 0.0 self.pressure = 0.0 return def registerobserver (self, observer): Self.observers. APPEND (Observer) return Def REMOVEOBSERVER (self, observer): SELF.OBSERVERS.REMOVE (Observer) RE 
    Turn def gettemperature (self): return self.temperature def gethumidity (self): return self.humidity 
        def getpressure (self): return self.pressure def measurementschanged (self): Self.notifyobservers () Return defSetmesurement (self, temp, humidity, pressure): Self.temperature = temp self.humidity = Humidity SE Lf.pressure = Pressure self.measurementschanged () return def notifyobservers (self): for item I N Self.observers:item.update (self.temperature, self.humidity, self.pressure) return class Currentcon Ditiondisplay (Observer): Def __init__ (self, weatherdata): Self.weatherdata = Weatherdata Self.temperat Ure = 0.0 self.humidity = 0.0 self.pressure = 0.0 weatherdata.registerobserver (self) retur
        n def update (self, temp, humidity, pressure): Self.temperature = temp Self.humidity = Humidity Self.pressure = Pressure Self.display () return def display (self): print ' temprature =%f, hum idity =%f. '% (self.temperature, self.humidity) return class Statiticdisplay (Observer): de F __init__ (Self, weatherData): Self.weatherdata = weatherdata self.temperature = 0.0 self.humidity = 0.0 self.pres
        sure = 0.0 weatherdata.registerobserver (self) Return def update (self, temp, humidity, pressure):
        Self.temperature = Temp Self.humidity = Humidity Self.pressure = pressure Self.display () return def display (self): print ' statictic:t =%f, h =%f, pressure =%f. '% \ (self.temper Ature, Self.humidity, self.pressure) return weather = Weatherdata () display = Currentconditiondisplay (weathe
 R) weather.setmesurement (2.0, 3.0, 4.0) display = Statiticdisplay (weather) weather.setmesurement (3.0, 4.0, 5.0)



Related Article

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.