Example of the Java observer pattern

  • 2020-04-01 04:28:22
  • OfStack

The observer pattern is a behavior design pattern. The purpose of the observer pattern is that you are interested in the state of an object and want to be notified every time it changes. In Observer mode, the object observing the state of another object is called the Observer Observer, and the observed object is called the Subject observed.

Observer pattern Observer

  The observer pattern defines a one-to-many dependency in which multiple observer objects listen to a topic object simultaneously.

When the subject object changes state, all observer objects are notified so that they can update themselves automatically.

The composition of the observer pattern

Abstract topic role: holds all references to observer objects in a collection, and each abstract topic role can have any number of observers. Abstract topics provide an interface to add and remove observer roles. It is generally implemented with an abstract class and interface.

Abstract observer role: defines an interface for all concrete observers, updating itself when notified of the topic.

Specific subject role: notifies all registered observers when the internal state of the specific subject changes. Specific topic roles are usually implemented with a subclass.

  Concrete observer role: this role implements the updated interface required by the abstract observer role to coordinate its own state with that of the topic. Usually implemented with a subclass. If desired, the specific observer role can hold a reference to the specific subject role.

Application instance

The observer mode is illustrated by a program example:

First, define the abstract observer:


//Abstract observer role
public interface Watcher
{
  public void update(String str);
} 

Then define the abstract subject role, that is, the abstract observed, in which methods are declared (add, remove, notify the observer) :


//Abstract subject role, watched
public interface Watched
{
  public void addWatcher(Watcher watcher);
  public void removeWatcher(Watcher watcher);
  public void notifyWatchers(String str);
} 

Then define the specific observer:


public class ConcreteWatcher implements Watcher
{
  @Override
  public void update(String str)
  {
    System.out.println(str);
  }
} 

Then there are specific thematic roles:


import java.util.ArrayList;
import java.util.List;
public class ConcreteWatched implements Watched
{
  //Depository observer
  private List<Watcher> list = new ArrayList<Watcher>();
  @Override
  public void addWatcher(Watcher watcher)
  {
    list.add(watcher);
  }
  @Override
  public void removeWatcher(Watcher watcher)
  {
    list.remove(watcher);
  }
  @Override
  public void notifyWatchers(String str)
  {
    //The auto-invocation is actually invoked by the topic
    for (Watcher watcher : list)
    {
      watcher.update(str);
    }
  }
} 

Write test classes:


public class Test
{
  public static void main(String[] args)
  {
    Watched girl = new ConcreteWatched();
    Watcher watcher1 = new ConcreteWatcher();
    Watcher watcher2 = new ConcreteWatcher();
    Watcher watcher3 = new ConcreteWatcher();
    girl.addWatcher(watcher1);
    girl.addWatcher(watcher2);
    girl.addWatcher(watcher3);
    girl.notifyWatchers(" happy ");
  }
}

The above is an example of the Java observer pattern that this site shares with you. Hope you enjoy it.


Related articles: