Design Pattern Summary Observer Pattern (Observer)

       Observer mode (Observer) is to define a one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.

       Multiple information processing lists are saved through an abstract subject Subject, and multiple observers can be added to instantly notify the observers of the latest developments and then derive subclass objects to process specific messages and states.

Components of the Observer Pattern

  Abstract subject roles: Keep all references to observer objects in a collection, each abstract subject 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: define an interface for all concrete observers that update themselves when notified by the subject.

  Topic-specific roles: Notify all registered observers when the internal state of a specific topic changes. Concrete subject roles are usually implemented with a subclass.

 

  Concrete Observer Role: This role implements the update interface required by the abstract observer role in order to reconcile its own state with the state of the subject. Usually implemented with a subclass. A concrete observer role can hold a reference to a concrete subject role if desired.

Abstract Observer Watcher.java

public interface Watcher
{
    public void update(String str);

}
 Abstract Theme Watched.java
public interface Watched
{
    public void addWatcher(Watcher watcher);

    public void removeWatcher(Watcher watcher);

    public void notifyWatchers(String str);

}
 The observer implements ConcreteWatcher.java
public class ConcreteWatcher implements Watcher
{

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

}
 The observed subject implements ConcreteWatched.java
import java.util.ArrayList;
import java.util.List;

public class ConcreteWatched implements Watched
{
    // store the 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);
        }
    }

}
 Test class Test.java
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 observer mode is divided into push and pull.  The "push" method means that the Subject maintains a list of observers. Whenever an update occurs, the Subject will actively push the update message to each Observer.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988581&siteId=291194637