Observer Design Pattern In Java

  • In this design pattern, we can have one subject and one or more observer(s).
  • Observer objects can be registered to any subject it wants to observe.
  • Subject will maintain a list for observers registered to it.
  • If there is any state change in subject, all the registered observers will be notified and updated.
  • Observers can unregistered from the subject, it is observing at any time.
  • Unregistered observers will not be notified on change in state of subject.
  • An Observer can be registered with multiple subjects.

GoF Definition: Define one to many dependency between objects so that any changes in state of one object, all dependent objects are notified and updated.

Observer Pattern Example

Following is the example of Observer pattern implementation in Java. Here, we have created a generic interface with one method, update() which will be used to update any change occurred to the subject it is subscribed to.


public interface Observer {
	void update(String s);

Once the observer interface is declared with required method, we can implement it to create actual observer class by implementing the interface as shown below. Here, we are creating two observer classes ObserverA and ObserverB, as shown below.


public class ObserverA implements Observer {

	public void update(String s) {	
	System.out.println("A Observer String value of subject is: "+s);


public class ObserverB implements Observer {

	public void update(String s) {
	System.out.println("B Observer String value of subject is: "+s);

After the observers are created, we need a subject, to which observers have to be subscribed. For this, we will create a subject interface as shown below. This subject will contain methods to be registered or unregistered by any observer and also notify the observers, which are registered in case of any change occurs in subject’s state .


public interface Subject {
	void register(Observer o);
	void unRegister(Observer o);
	void notifyObservers(String s);

Now, we create a subject class named SubjectA which implements Subject interface as shown below. Our SubjectA class will have a list of observers, who are registered to it and a status field.

Subject class will also have implementation for register(), unregister() methods to register or un-register from the subject and also notifyObservers() method, which updates the registered observers upon change of status filed(
notice that setStatus() setter method calls notifyObservers() method ), by calling each observers update() method.


import java.util.ArrayList;
import java.util.List;

public class SubjectA implements Subject{

	List<Observer> observers = new ArrayList<>();
	String status;
	public String getStatus() {
		return status;

	public void setStatus(String status) {
		this.status = status;
		//Call NotifyAll method to update Observers

	public void register(Observer o) {

	public void unRegister(Observer o) {

	public void notifyObservers(String s) {
		observers.forEach(o -> o.update(s));

Finally, time for action 🙂 We will create a demo class with main method, to test our observer pattern. Here, we have created one subject and two observers registered to it. We change the status of subject to verify that observers get notified. Then we unregistered one of the observers and update the status of the subject to verify that the unregistered observer is not getting any updates from subject.


public class MultipleObserverPatternExample {

	public static void main(String args[]) {
		//Create Subject
		SubjectA subject = new SubjectA();
		//Create instances of observers
		ObserverA oA = new ObserverA();
		ObserverB oB = new ObserverB();
		//Register the observers with Subject
		//Change subject status
		System.out.println("Changing Subject status!!");
		//Unregister ObserverB from Subject
		//Change the status of Subject again
		System.out.println("Changing Subject status again!!");
		subject.setStatus("Bye Bye!!");


You may be interested in following articles