Chapter 2. Keeping your Objects in the Know: The Observer Pattern

Images

You don’t want to miss out when something interesting happens, do you? We’ve got a pattern that keeps your objects in the know when something they care about happens. It’s the Observer Pattern. It is one of the most commonly used design patterns, and it’s incredibly useful. We’re going to look at all kinds of interesting aspects of Observer, like its one-to-many relationships and loose coupling. And, with those concepts in mind, how can you help but be the life of the Patterns Party?

Congratulations!

Your team has just won the contract to build Weather-O-Rama, Inc.’s next-generation, internet-based Weather Monitoring Station.

Images

The Weather Monitoring application overview

Let’s take a look at the Weather Monitoring application we need to deliver—both what Weather-O-Rama is giving us, and what we’re going to need to build or extend. The system has three components: the weather station (the physical device that acquires the actual weather data), the WeatherData object (that tracks the data coming from the Weather Station and updates the displays), and the display that shows users the current weather conditions:

The WeatherData object was written by Weather-O-Rama and knows how to talk to the physical Weather ...

Get Head First Design Patterns, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.