Menu

Observer Pattern

Classification: Behavioral patterns
Description : Define a one-to-many dependency between objects where a state change in one object results in all its dependents being notified and updated automatically.

This pattern is sometimes referred to as Listeners in some frameworks.

Read about the pattern here.
The code sample can be found in my git repo.

In the example code I have done, there is a city, as with all cities many things can go wrong in a city. So city is the subject or the observable. To handle the emergency situations we have many emergency handlers. These handlers observe the city and take actions when needed. So handlers are Observers.
When something happens in the city, all observers will be notified. Some observers respond to the situation and some do not.