Java observer design pattern details

  • 2020-05-05 11:15:03
  • OfStack

The     observer pattern (sometimes referred to as the publish (publish) -subscribe (Subscribe) pattern, model-view (View) pattern, source-listener (Listener) pattern, or slave pattern) is one of the software design patterns. In this mode, a target object manages all observer objects that are dependent on it, and actively notifies when its own state changes. This is usually done by calling the method provided by each observer. This pattern is often used to implement event handling systems.

  observer mode (Observer) perfectly separates the observer from the observed object. For example, the user interface can act as an observer, and the business data is the observed. The user interface observes the changes in the business data, and when it finds the changes, it is displayed on the interface. One of the principles of object-oriented design is that each class in the system focuses on one feature and not the other. An object does one thing and does it well. The observer pattern draws a clear line between modules, improving the maintainability and reusability of applications.
The observer design pattern defines a one-to-many dependency between objects so that when an object's state changes, all dependent objects are notified and automatically refreshed.
implementation:
There are many implementations of the observer pattern, and essentially, the pattern must contain two roles: the observer and the observed. In the example above, the business data is the observed object and the user interface is the observer. There is a logical relation of "observation" between the observer and the observed. When the observed changes, the observer will observe such changes and respond accordingly. If you use such an observation process between the user interface and the business data, you can ensure that the interface and the data are clearly demarcated, assuming that the requirements of the application have changed, the interface's performance needs to be modified, and only a user interface needs to be rebuilt, and the business data does not need to change.
1, observer

(Observer) registers itself with the observed object (Subject), which stores the observer in a container (Container).

2, observed

The observed object changes in some way (SomeChange as shown in the figure), all registered observers are retrieved from the container, and the change is notified to the observer.

3, unobserve

The observer tells the observed to undo the observation, and the observed removes the observer from the container.
When the observer registers himself in the observed's container, the observed should not ask about the specific type of the observer, but should instead use the observer's interface. This has the advantage of assuming that there is another observer in the program, as long as that observer is the same interface implementation. An observed can correspond to multiple observers, and when the observed changes, he can inform all observers one by one. Based on the interface, not the implementation - this gives the program more flexibility.

demonstration code:
Defines the role abstract class to be observed :


package test.edu.mainrole; 
 
import java.util.ArrayList; 
 
public abstract class AbsRole { 
 
  private ArrayList<IObserver> list = new ArrayList<IObserver>(); 
 
  public void add(IObserver observer) { 
    list.add(observer); 
 
  } 
 
  public void delete(IObserver observer) { 
    list.remove(observer); 
  } 
 
  public void nodifyObservers(String newState) { 
    for (IObserver observer : list) { 
      observer.update(newState); 
    } 
  } 
} 

Subclass of observed characters :


package test.edu.mainrole; 
 
public class Role extends AbsRole { 
 
  private String state; 
 
  public String getState() { 
    return state; 
  } 
 
  public void change(String nupdate) { 
    state = nupdate; 
    this.nodifyObservers(state); 
  } 
} 

Define the observer interface :


package test.edu.mainrole; 
 
public interface IObserver { 
  public void update(String newState); 
     
} 

Specific observer :


package test.edu.mainrole; 
 
public class ObserverObj1 implements IObserver{ 
 
  private String observerState; 
 
  @Override 
  public void update(String state) { 
    observerState = state; 
    System.out.println(" The observer 1 Is: " + observerState); 
  } 
 
} 

package test.edu.mainrole; 
 
public class ObserverObj2 implements IObserver { 
   
  private String observerState; 
 
  @Override 
  public void update(String state) { 
    observerState = state; 
    System.out.println(" The observer 2 Is: " + observerState); 
  } 
} 

Test client :


package test.edu.mainrole; 
 
public class Client { 
 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
 
    Role subject = new Role(); 
    IObserver observer1 = new ObserverObj1(); 
    IObserver observer2 = new ObserverObj2(); 
    subject.add(observer1); 
    subject.add(observer2); 
    subject.change("update!"); 
  } 
} 

Result :


 The observer 1 Is: update!

 The observer 2 Is: update!

The above is the entire content of this article, I hope to inspire you to learn.


Related articles: