Home>

The observer pattern is a behavioral design pattern.The purpose of the observer mode is,When you are interested in the state of an object,Want to be notified every time it changes.In observer mode,An object observing the state of another object is called an observer observer, and an observed object is called a subject observer.

Observer mode

The observer pattern defines a one-to-many dependency relationship,Have multiple observer objects listen to a topic object at the same time.

When this theme object changes state,Will notify all observers,Let them update themselves automatically.

The composition of the observer pattern

Abstract theme role:keep all references to observer objects in a collection,Each abstract theme character can have any number of observers.The abstract theme provides an interface,You can add and delete observer roles.Usually implemented with an abstract class and interface.

Abstract observer role:define an interface for all concrete observers,Update yourself when notified about topics.

Specific theme role:When the internal status of a specific theme changes,Give notice to all registered observers.Specific theme roles are usually implemented in a subclass.

Specific observer role:This role implements the update interface required by the abstract observer role,In order to reconcile the state of the subject with the state of the subject.Usually implemented with a subclass.If needed,A specific observer role can hold a reference to a specific topic role.

Program example

Illustrate the observer mode through a program example:

First define the abstract observer:

//abstract observer role
public interface watcher
{
  public void update (string str);
}

Then define the abstract theme role,The abstract observerDeclare methods in it (add, remove observers,Notify the observer):

//Abstract theme characters,watched:be watched
public interface watched
{
  public void addwatcher (watcher watcher);
  public void removewatcher (watcher watcher);
  public void notifywatchers (string str);
}

Then define specific observers:

public class concretewatcher implements watcher
{
  @override
  public void update (string str)
  {
    system.out.println (str);
  }
}

Followed by specific theme roles:

import java.util.arraylist;
import java.util.list;
public class concretewatched implements watched
{
  //store 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 automatic call is actually called by the theme
    for (watcher watcher:list)
    {
      watcher.update (str);
    }
  }
}

Write test class:

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 we share with you,Hope you like it.

  • Previous MySQL LEFT JOIN table join advanced learning tutorial
  • Next Detailed Java reflection mechanism