This article explains Observer design pattern in java with UML class diagram. It then takes an example scenario in java and explains it with class diagram and code.
Introduction: Observer Design Pattern is a behavioral design pattern among the Gang Of Four(GOF) Design PatternsArticle on GOF Patterns & their types. Being a behavioral design pattern, the Observer pattern deals with how objects of the designed system interact with each other.
What is Observer Design Pattern
Observer Design Pattern is used when there are multiple subscribers observing updates from a publisher. The publisher, known as Subject, publishes its change of state to its subscribers. Subscribers, known as Observers, receive these changes and update themselves accordingly.
Scenarios in which Observer Design Pattern can be used
- Change of an object requires changes to multiple others: When there is an observed object(a.k.a the Subject), then there should be a mechanism to notify its changes to multiple of its observer components which have evinced interest in listening to these changes without the exact quantity of observers being known at design time.
- Notification capability needed without tight rules regarding how this notification is used: When the different Observers of notifications from a Subject have there own presentation and\or business logic implementation, but still need to have the capability of getting the latest updates from Subject. I.e. Observers are loosely coupled with the Subject
Class Diagram for Observer Design Pattern
Explanation of Observer Design Pattern’s Class Diagram
Subjectis the one being observed. It has
detach()methods to add or remove observers respectively. It uses
notify()method to publish its changes to observers\subscribers.
Observerobjects register\attach themselves to the
Subjectthey want to listen to for updates.
ConcreteObserverinstances are actual
Observerobjects which treat the notifications they receive from the
Subjectin their own individual ways.
ConcreteSubjectis the actual
Subjectobject whose state change is notified to multiple observers.
Inbuilt Observer Pattern in Java
Java has an inbuilt Observer pattern implementation using the class
java.util.Observable (represents Subject) and the interface
java.util.Observer(represents an Observer). Concrete Observers in Java need to implement the
Observer interface, whereas concrete Subject needs to extend
Observable to provide its own notification logic.
The following class diagram shows the relationship between
Observable as defined in the Java SDK follows –
Lets now look at an example implementation of observer design pattern in Java using
Observer Design Pattern Example Implementation in Java-Class Diagram
Code for the classes shown in Java Example’s Class Diagram
The Java class diagram above depicts Observer Design pattern implemented for a simple Publish-Subscribe implementation. Let us now go through whats there in Java’s example’s class diagram & corresponding code –
Publisheris the Subject. It extends
Subscriber2are the Observers. They implement
Publisher. It then adds one instance each of
Subscriber2to Publisher’s list of Observers.
Clientthen invokes method
changeStateTo()with new state value as “assigned”. Internally
notifyObservers()with this new state value. Before notifying Publisher calls
setStateChanged()which is a requirement of the java’s observer pattern implementation.
update()methods of Subscriber1 and Subscriber 2 are called internally by the
notifyObservers()method and the new state value received by both in the parameter
In the above tutorial we understood what is Observer design pattern and the main scenarios in which this pattern is applicable. We then looked at the UML class diagram for Observer Design Pattern & its explanation, a Java Use Case implementing Observer pattern with its class diagram and code for the classes shown in the class diagram, followed by explanation of both the class diagram & code. This concludes the tutorial on Observer design pattern.