Observer Design Pattern in Java

Observer mode

When there is a one-to-many relationship between objects, the Observer Pattern is used. For example, when an object is modified, it will automatically notify its dependent object. The observer mode is a behavioral mode.


Intent: defines a one-to-many dependency between objects. When an object's state changes, all objects that depend on it are notified and automatically updated.

Main solution: An issue where object state changes are notified to other objects, and easy to use and low coupling are considered to ensure a high degree of collaboration.

When to use: The state of an object (target object) changes, and all dependent objects (observer objects) will be notified and broadcasted.

How to solve: Using object-oriented technology, this dependency can be weakened.

Key Code: There is an ArrayList in the abstract class that holds the observers.

Application example: 1. At the time of the auction, the auctioneer observes the highest price and then informs other bidders to bid. 2. In the Journey to the West, Goku asked Bodhisattva to surrender to the red baby. The Buddha sprinkled a water to recruit an old turtle. This turtle is an observer. He observes the action of the Buddha sprinkling water.

Advantages: 1. The observer and the observed person are abstractly coupled. 2. Establish a trigger mechanism.

Disadvantages: 1. If an observed object has many direct and indirect observers, it will take a lot of time to notify all observers. 2. If there is a circular dependency between the observer and the observed target, the observation target will trigger a circular call between them, which may cause the system to crash. 3. Observer mode There is no corresponding mechanism for the observer to know how the observed target object has changed, but only to know that the observation target has changed.

Usage scenarios:

  • An abstract model has two aspects, one of which depends on the other. These aspects are encapsulated in separate objects so that they can be changed and reused independently of each other.
  • An object change will cause other one or more objects to change as well, without knowing how many objects will change, which can reduce the degree of coupling between objects.
  • An object must notify other objects without knowing who they are.
  • You need to create a trigger chain in the system. The behavior of the A object will affect the B object. The behavior of the B object will affect the C object.... You can use the observer mode to create a chain trigger mechanism.

Note: 1. There is already a support class for observer mode in JAVA. 2. Avoid circular references. 3. If the order is executed, an observer error will cause the system to jam, generally in an asynchronous manner.


Observer mode uses three classes, Subject, Observer, and Client. The Subject object has methods for binding the observer to the Client object and unbinding the observer from the Client object. We create the Subject class, the Observer abstract class, and the entity class that extends the abstract class Observer.

ObserverPatternDemo, our demo class uses Subject and entity class objects to demonstrate the observer pattern.


Step 1

Create a Subject class.

import java.util.ArrayList; import java.util.List; public class Subject { private List<Observer> observers = new ArrayList<Observer>(); private int state; public int getState() { return state; } public void setState(int state) { this.state = state; notifyAllObservers(); } public void attach(Observer observer){ observers.add(observer); } public void notifyAllObservers(){ for (Observer observer : observers) { observer.update(); } } }

Step 2

Create the Observer class.

public abstract class Observer { protected Subject subject; public abstract void update(); }

Step 3

Create an entity observer class.

public class BinaryObserver extends Observer{ public BinaryObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println( "Binary String: " + Integer.toBinaryString( subject.getState() ) ); } }

public class OctalObserver extends Observer{ public OctalObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println( "Octal String: " + Integer.toOctalString( subject.getState() ) ); } }

public class HexaObserver extends Observer{ public HexaObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println( "Hex String: " + Integer.toHexString( subject.getState() ).toUpperCase() ); } }

Step 4

Use Subject and entity observer objects.

public class ObserverPatternDemo { public static void main(String[] args) { Subject subject = new Subject(); new HexaObserver(subject); new OctalObserver(subject); new BinaryObserver(subject); System.out.println("First state change: 15"); subject.setState(15); System.out.println("Second state change: 10"); subject.setState(10); } }

Step 5

Executing the program, outputting results:

First state change: 15
Hex String: F
Octal String: 17
Binary String: 1111
Second state change: 10
Hex String: A
Octal String: 12
Binary String: 1010

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition