• 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.

Observer.java

package com.asb.multiple.observer;

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.

ObserverA.java

package com.asb.multiple.observer;

public class ObserverA implements Observer {

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

ObserverB.java

package com.asb.multiple.observer;

public class ObserverB implements Observer {

	@Override
	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 .

Subject.java

package com.asb.multiple.observer;

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.

SubjectA.java

package com.asb.multiple.observer;

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
		notifyObservers(status);
	}

	@Override
	public void register(Observer o) {
		observers.add(o);
	}

	@Override
	public void unRegister(Observer o) {
		observers.remove(o);
	}

	@Override
	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.

MultipleObserverPatternExample.java

package com.asb.multiple.observer;

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
		subject.register(oA);
		subject.register(oB);
		
		//Change subject status
		System.out.println("Changing Subject status!!");
		subject.setStatus("Hello!!");
		System.out.println();
		
		//Unregister ObserverB from Subject
		subject.unRegister(oB);
		
		//Change the status of Subject again
		System.out.println("Changing Subject status again!!");
		subject.setStatus("Bye Bye!!");
	}
}

Output

You may be interested in following articles