Java Observer Pattern Example _java

Source: Internet
Author: User

The observer pattern is a behavioral design pattern. The purpose of the observer pattern is that when you are interested in the state of an object, you want to be notified each time it changes. In observer mode, an object that observes the state of another object is called a observer observer, and the object being observed is called the subject observer.

Observer Mode Observer

The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to one subject object at the same time.

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

The composition of the Observer pattern

Abstract theme roles: All references to observer objects are kept in a collection, and each abstract theme role can have any number of observers. Abstract topics provide an interface to add and remove observer roles. It is generally implemented using an abstract class and interface.

Abstract Observer role: Defines an interface for all specific observers, updating themselves when notified of the subject.

Specific theme roles: Notify all registered observers of changes in the internal state of a specific subject. Specific theme roles are typically implemented with a subclass.

Specific observer role: The role implements the update interface required by the abstract observer role to reconcile its own state with the state of the subject. Typically implemented with a subclass. If necessary, the specific observer role can hold a reference to a specific theme role.

Program instance

The observer pattern is illustrated by a program instance:

First, define the abstract observer:

Abstract observer role public
interface watcher
{public
  void update (String str);

The abstract theme role is then defined, which is the abstract observer, in which the method is declared (adding, removing the observer, notifying the observer):

Abstract theme roles, watched: Observed 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);
  

The following are the specific theme roles:

Import java.util.ArrayList;
Import java.util.List;
Public class Concretewatched implements watched
{
  //store observer
  private list<watcher> List = new arraylist& Lt 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)
  {
    //automatic invocation is actually a for
    (watcher Watcher:list) called by the theme
    {
      watcher.update (str);
    }
  }

To write a 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 a small set to share the Java Observer Model example, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.