Design Pattern: Observer Pattern in Java
Tags: Design pattern, Java, Observer PatternWe use simple examples and diagrams to describe the concept of observer pattern. We also give a simple implementation of the observer pattern in Java.
What is Observer Pattern
The observer pattern defines a one-to-many relationship between two kinds of objects. The one side is called Subject, which maintains a list of Objects (called observers), and automatically notify each of the observers if something interesting happens.
The definition of the Observer pattern provided in the GoF book, Design Patterns: Elements of Reusable Object-Oriented Software, is:
“One or more observers are interested in the state of a subject and register their interest with the subject by attaching themselves. When something changes in our subject that the observer may be interested in, a notify message is sent which calls the update method in each observer. When the observer is no longer interested in the subject’s state, they can simply detach themselves.”
Steps to use Observer Pattern
- We create two kinds of Objects: Subject and Observers.
- When we create an observer, we register it to the subject.
- When something interesting happens to the subject, the subject will notify all the observers that have been registered.
- When a particular observer is no longer needed to be notified, the subject can remove it from the list of the observers.
- See the following diagram to understand how server Pattern works.
See the following diagram to understand how Observer Pattern works.
Implement Observer Pattern In Java
Step 1
We create a Subject class, in which there is a container to hold all the Observers that will be registered to this subject. If something interesting happens, the state of the subject will change, and the registered Observers will be notified by calling the observer’s perform() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.ArrayList; import java.util.List; class Subject { private List<Observer> observers = new ArrayList<Observer>(); private String state; public String getState() { return state; } public void somethingInterestingHappen(String state) { this.state = state; notifyAllObservers(); } public void register(Observer observer){ observers.add(observer); observer.setSubject(this); } public void notifyAllObservers(){ for (Observer observer : observers) { observer.perform(); } } } |
Step 2
We create two kinds of Observers: ObserverA and ObserverB. Both extend the super class Observer, which has an abstract method perform(). The subClass will override this method to perform something specifically based on the subject’s updated state.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
abstract class Observer { protected Subject subject; public Observer(){} public void setSubject(Subject sub) { this.subject = sub; } public abstract void perform(); } class ObserverA extends Observer{ public ObserverA(){} public ObserverA(Subject subject){ this.subject = subject; this.subject.register(this); } @Override public void perform() { System.out.println( "In ObserverA " + subject.getState() ); } } class ObserverB extends Observer{ public ObserverB(){} public ObserverB(Subject subject){ this.subject = subject; this.subject.register(this); } @Override public void perform() { System.out.println( "In ObserverB-------------- " + subject.getState() ); } } |
Step 3
In the driver class, we create a subject, and register two observers to it. Then we change the subject’s state twice, you can see the observer’s perform() methods are called after the state change.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class ObserverPattern { public static void main(String[] args) { Subject subject = new Subject(); subject.register(new ObserverA()); subject.register(new ObserverB()); System.out.println("First State: We have some good news"); subject.somethingInterestingHappen("We have some good news"); System.out.println("Second state: Oh, something wrong"); subject.somethingInterestingHappen("Oh, something wrong"); } } |
Here is the output after executing the program:
|
1 2 3 4 5 6 |
First State: We have some good news In ObserverA We have some good news In ObserverB-------------- We have some good news Second state: Oh, something wrong In ObserverA Oh, something wrong In ObserverB-------------- Oh, something wrong |
Reference: http://javaphpmysql.blogspot.com/2012/12/java-very-simple-observer-pattern.html











