Observer Design Pattern In Java
Join the DZone community and get the full member experience.Join For Free
Today, I will discuss simple and very useful behavioral design pattern called — Observer Design Pattern. This design pattern is useful when we want get notified about changes in the object state.
Observer Design Pattern
- The Observer Design Pattern maintains one-to-many dependency between Subject (Observable) and its dependents (Observer) in such a way that whenever state of Subject changes, its dependents get notified.
- The Observer Design Pattern is a design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
- The Observer Design Pattern is used when we like to have notification upon changes in the objects state.
- The Observer Design Pattern is one of twenty-three well known Gang of Four design patterns that defines an one-to-many dependency objects so that when one object changes state, all of its dependents get notified and updated automatically.
- The Observer Design Pattern is mainly used to implement distributed event handling , in "event driven" system.
- In such systems, the subject is usually called a "source of events", while the observers are called "sink of events".
- Most modern languages have built-in "event" constructs which implement the observer pattern components.
- Java also offers Observer and Observable to support built-in event constructs for implementing observer pattern. These are available since Java 1.
- But, in Java 9 these are declared deprecated/obsolete because the event model supported by
Observableis quite limited, the order of notifications delivered by
Observableis unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using the
java.beanspackage. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the
java.util.concurrentpackage. For reactive streams style programming, see the
FlowAPI. (read more on Deprecate Observer and Observable).
Now an example to understand the implementation of Observer Design Pattern.
Suppose there are some public figures like politicians or celebrities for which there are some followers. Whenever these public figures do any tweet, there registered followers get the notification on that.
Tweet Notification Example using Observer Design Pattern
First we will define the Subject interface:
Then we will create Observer interface:
Then we will create concrete subject class called PublicFigure:
Please notice that we have APIs for adding and removing followers. We also have API for notifying followers. And we also have API to tweet. The tweet API does notify to its registered followers. While notifying followers, the subject also provides information regarding the change done. In our example its tweet which changes. So, it's in the parameter of notification. So, in short Subject should also give the context of change to its followers for notification. Without that it observer pattern will not be very effective. Every subject maintains it's list of followers.
Now we will define Follower class:
Now, its time to write a Main program to execute and test the output:
And here's the output:
Please note that when I unregistered 'Racheal' from Barak Obama's follower list, she stopped receiving notifications.
Please do not think that this is how the Tweeter works :)
This is very small example to demonstrate the Observer Design Pattern structure, code and function.
Tweeter has an actual production system which is capable to maintain millions of followers for each user. So that does not suite this kind of synchronous notification method. There are many more components involve to make the tweeter operational.
That's all! I hope we are clear on implementation of Observer Design Pattern.
Source Code can be found here: Observer Design Pattern Sample Code
I hope this tutorial demonstrates the use of command design pattern.
Liked the article? Please don't forget to press that like button. Happy coding!
Need more articles, please visit my profile: Brijesh Saxena.
Opinions expressed by DZone contributors are their own.