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.