Diving into the Observer Pattern
Diving into the Observer Pattern
Welcome back! After exploring the Strategy Pattern, it's time to delve into another important behavioral design pattern: the Observer Pattern. This pattern focuses on establishing one-to-many relationships between objects, allowing an object (subject) to notify other objects (observers) about changes in its state.
What You'll Learn
In this lesson, you will discover how to implement the Observer Pattern in Go, covering the following key concepts:
- Defining Observers: You will learn how to define an interface for observers so they can receive updates from the subject when something changes.
- Creating a Subject: Next, you will see how to create a concrete subject that maintains a list of observers and provides methods to register, unregister, and notify them.
- Putting It All Together: Finally, you will combine these elements to create a functional example where the subject notifies multiple observers of state changes.
Let's look at an example to illustrate these concepts:
We start by defining the Observer interface with an Update method that observers must implement:
Next, we create a concrete observer that implements the Observer interface. When the Update method is called, the observer receives a message from the subject:
Now, let's define the Subject interface and a concrete subject that implements it. The subject maintains a list of observers and provides methods to register, unregister, and notify them. When the NotifyObservers method is called, the subject sends a message to all observers by calling their Update methods, thereby notifying them of the state change:
Finally, we can put it all together in the main function:
Let's break down this example:
- We define the
Observerinterface with anUpdatemethod that observers must implement. - The
ConcreteObserverstruct implements theObserverinterface and prints the received message when theUpdatemethod is called. - The
Subjectinterface defines methods to register, unregister, and notify observers. - The
ConcreteSubjectstruct maintains a list of observers and implements theSubjectinterface methods. InUnregisterObserver, we filter the list to remove all instances of the specified observer, ensuring the operation is safe even if the observer isn't found or the list is empty. - In the
mainfunction, we create two observers (subscriber1andsubscriber2) and a subject (weatherStation). - We register the observers with the subject, notify them of a state change, and then unregister one of the observers before sending another notification.
- When the subject notifies the observers, they receive the message and print it to the console with their respective names and IDs.
The output of the example will look like this:
Notice how the observers receive the updates from the subject and react accordingly. This demonstrates the one-to-many relationship established by the Observer Pattern.
Let's understand what are the key components of the Observer Pattern:
- Subject: Represents the object being observed. It maintains a list of observers and provides methods to register, unregister, and notify them. In our example,
Subjectis the interface that subjects must implement, andConcreteSubjectis a concrete subject that implements this interface. - Observer: Defines an interface for objects that should be notified of changes in the subject. In our example,
Observeris the interface that observers must implement, andConcreteObserveris a concrete observer that implements this interface.
By following this example, you will gain a solid understanding of how the Observer Pattern works and how it can be applied in real-world scenarios.
