The Observer pattern defines a one-to-many dependency between objects such that when one object, the subject, changes state, all its dependents, known as observers, are automatically notified and updated. The subject object notifies the observers by calling one of their methods.
The observer pattern is a behavioral pattern that is used to form relationships between objects at runtime and it’s a classic standard in decoupling. IN this article, we discuss what is observer design pattern in java and its implementation.
Implementation of Observer Pattern in Java
In java, the observer pattern is implemented in the java.util package.
The subject is implemented by creating a class with extends the java.util.Observable class while the observer is created by implementing the Observer interface in the same package.
Important methods of the Observable class;
The Observable class has the following important methods which simplify the implementation of the observer pattern
- addObserver: This method adds an observer in the list of the observers to be notified by the subject when its status changes.
- deleteObserver: This method is used to remove an observer from the list of observers to be notified of the status change by a given subject.
- hasChanged: This method is used to check whether the status of a subject has changed.
- clearChanged: This method is used indicate that the subject has no changes or all the observers have been notified when changes occurred.
- notifyObservers: This method is used to notify all the observers if the subject has changed.
- Important methods of the Observer Interface
Update: This method is called when the subject is changed. An implementation of this method contains the behavior of the observer when the subject changes.
Own Implementation of the Observer Pattern
If a developer does not want to use the observer pattern as implemented in java.util package, they can develop their own interfaces which implement this pattern. This can be done by writing code for subject and observer interfaces which contain methods for performing the various operations and implementing the interfaces in their code. The subject interface, in this case, contains all the methods necessary for adding, removing and notifying observers while the observer interface will have the update method that can be called by the subject implementation when its status changes.
Advantage and disadvantages of Observer Design Pattern in Java
The Primary advantage of the observer design pattern is that it allows loose coupling: The observer pattern enables the developer to implement loose coupling between observers and subjects. The subject only knows the list of observers and it is not concerned about how the individual observers have been implemented.
One of the disadvantages of the observer design pattern is increased complexity in debugging. If not properly implemented, the observer pattern can complicate the debugging exercise since the flow of control is implicit between subjects and observers. In addition, the pattern may complicate memory management since the subject holds the reference for an observer until the observer is deregistered.