Observer Pattern-What It Is and How To Use It
Do you want to be a millionaire? Then you are probably in the right place. Because today we’ll reveal a secret to become it. Yes, we are going to learn the observer pattern by using an example of a stock market. We are going to subscribe to a few stocks that will notify us when their price drops below a certain threshold as well as it will also notify when it rises beyond our expectations. You might think that probably, it’s not that easy to earn money from the stock market, I agree, but the observer pattern is pretty simple to understand. It’s a powerful tool that you’ll often end up using routinely as a developer. Enough motivation, let’s get started!
Observer pattern = Publishers + Subscribers
Assume that you have an object called X, that contains data. And you have a list of other objects that want to get notified when the data inside X changes. That is when we should use the observer pattern.
For instance, in our stock market example, a stock will be our subject, and mobile apps or software that display the price of the stock can be called an observer. Let’s implement the implementation!
First, we’ll focus on how to implement the subject.
- Created an interface Stock which will be our subject that contains methods to add, remove, and notify the observers.
- Create a concrete implementation of our subject, which is the actual stock that we are interested in — Tesla! It maintains the list of observers that need to be notified when the price of the stock changes. And the implementation of methods that we defined in the interface.
Now let’s see how to implement observers.
- Will create an interface that contains a common updatePrice() method. All the observers implementing the interface will have to override updatePrice() method.
- Now create a concrete implementation of observer. Like we have defined AppDisplay class. It contains the reference of the subscriber as we want to register or unregister the observer.
Okay, we are almost done! Let’s quickly see how we can use it.
That was it! Sounds easy right. Let’s have a look at the formal definition of the observer pattern.
According to the Head First Design Pattern, the observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
Whenever we are working with multiple objects dealing with each other’s data, we should always strive for loose coupling between the two objects.The biggest benefit of using this pattern is it promotes the loose coupling between objects namely the subject and the observer. You might be thinking how? Here is why.
- The only thing subscriber knows about observer is that it should implement the observer interface, that’s all. Rest of the functionality is completely unknown to the subscriber.
- We can add new observers any time without changing our main code. Wow!
That’s all. Hope you’ll make some money from Tesla or any other your favorite stock! Haha
Thanks for reading till now,