Observer Design Pattern In Java
Join the DZone community and get the full member experience.
Join For FreeToday, 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
Observer
andObservable
is quite limited, the order of notifications delivered byObservable
is unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using thejava.beans
package. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in thejava.util.concurrent
package. For reactive streams style programming, see theFlow
API. (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:
xxxxxxxxxx
package org.trishinfotect.observer;
public interface Subject {
public void addSubscriber(Observer observer);
public void removeSubscriber(Observer observer);
public void notifySubscribers(String tweet);
}
Then we will create Observer interface:
xxxxxxxxxx
package org.trishinfotect.observer;
public interface Observer {
public void notification(String handle, String tweet);
}
Then we will create concrete subject class called PublicFigure:
xxxxxxxxxx
package org.trishinfotect.observer;
import java.util.ArrayList;
import java.util.List;
public class PublicFigure implements Subject {
protected List<Observer> observers = new ArrayList<Observer>();
protected String name;
protected String handle;
public PublicFigure(String name, String handle) {
super();
this.name = name;
this.handle = "#" + handle;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHandle() {
return handle;
}
public void tweet(String tweet) {
System.out.printf("\nName: %s, Tweet: %s\n", name, tweet);
notifySubscribers(tweet);
}
public synchronized void addSubscriber(Observer observer) {
observers.add(observer);
}
public synchronized void removeSubscriber(Observer observer) {
observers.remove(observer);
}
public void notifySubscribers(String tweet) {
observers.forEach(observer -> observer.notification(handle, tweet));
}
}
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:
xxxxxxxxxx
package org.trishinfotect.observer;
public class Follower implements Observer {
protected String name;
public Follower(String name) {
super();
this.name = name;
}
public void notification(String handle, String tweet) {
System.out.printf("'%s' received notification from Handle: '%s', Tweet: '%s'\n", name, handle, tweet);
}
}
Now, its time to write a Main program to execute and test the output:
xxxxxxxxxx
package org.trishinfotect.observer;
public class Main {
public static void main(String args[]) {
PublicFigure bobama = new PublicFigure("Barack Obama", "bobama");
PublicFigure nmodi = new PublicFigure("Narendra Modi", "nmodi");
Follower ajay = new Follower("Ajay");
Follower vijay = new Follower("Vijay");
Follower racheal = new Follower("Racheal");
Follower micheal = new Follower("Micheal");
Follower kim = new Follower("Kim");
bobama.addSubscriber(ajay);
bobama.addSubscriber(vijay);
bobama.addSubscriber(racheal);
bobama.addSubscriber(micheal);
bobama.addSubscriber(kim);
nmodi.addSubscriber(ajay);
nmodi.addSubscriber(vijay);
nmodi.addSubscriber(racheal);
nmodi.addSubscriber(micheal);
nmodi.addSubscriber(kim);
bobama.tweet("Hello Friends!");
nmodi.tweet("Vande Matram!");
bobama.removeSubscriber(racheal);
bobama.tweet("Stay Home! Stay Safe!");
}
}
And here's the output:
Name: Barack Obama, Tweet: Hello Friends!
'Ajay' received notification from Handle: '#bobama', Tweet: 'Hello Friends!'
'Vijay' received notification from Handle: '#bobama', Tweet: 'Hello Friends!'
'Racheal' received notification from Handle: '#bobama', Tweet: 'Hello Friends!'
'Micheal' received notification from Handle: '#bobama', Tweet: 'Hello Friends!'
'Kim' received notification from Handle: '#bobama', Tweet: 'Hello Friends!'
Name: Narendra Modi, Tweet: Vande Matram!
'Ajay' received notification from Handle: '#nmodi', Tweet: 'Vande Matram!'
'Vijay' received notification from Handle: '#nmodi', Tweet: 'Vande Matram!'
'Racheal' received notification from Handle: '#nmodi', Tweet: 'Vande Matram!'
'Micheal' received notification from Handle: '#nmodi', Tweet: 'Vande Matram!'
'Kim' received notification from Handle: '#nmodi', Tweet: 'Vande Matram!'
Name: Barack Obama, Tweet: Stay Home! Stay Safe!
'Ajay' received notification from Handle: '#bobama', Tweet: 'Stay Home! Stay Safe!'
'Vijay' received notification from Handle: '#bobama', Tweet: 'Stay Home! Stay Safe!'
'Micheal' received notification from Handle: '#bobama', Tweet: 'Stay Home! Stay Safe!'
'Kim' received notification from Handle: '#bobama', Tweet: 'Stay Home! Stay Safe!'
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.
Comments