Design Pattern-Observer Pattern

Source: Internet
Author: User

The observer mode is the behavior mode of an object. It is also called the Publish/Subscribe mode, Model-View mode, and Source-Listener (Source/Listener) mode) mode or Dependents mode. The observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.
Below is a simple observer mode:
Subject. java

Package com. wsds. test; import java. util. arrayList; import java. util. list;/*** abstract topic class * @ author Administrator **/public abstract class Subject {// used to save the private List of objects registered with the observer
 
  
List = new ArrayList
  
   
();/*** Register the observer object * @ param Observer observer object */public void attach (observer Observer) {list. add (observer); System. out. println ("Attached an obsever");}/*** Delete the observer object * @ param Observer observer object */public void detach (observer Observer) {list. remove (observer);} public void policyobservers (String newState) {for (Observer observer: list) {observer. update (newState );}}}
  
 
ConcreteSubject. java
Package com. wsds. test;/*** Subject-specific corner class * @ author Administrator **/public class ConcreteSubject extends Subject {private String state; public String getState () {return state ;} public void change (String newState) {state = newState; System. out. println ("the topic status is:" + state); // The status changes and each observer is notified of this. yyobservers (state );}}
Observer. java
Package com. wsds. test; public interface Observer {/*** update interface * @ param state update status */public void update (String state );}
ConcreteObserver. java
Package com. wsds. test; public class ConcreteObserver implements Observer {// private String observerState of the Observer; @ Overridepublic void update (String state) {/** update the Observer state, make it consistent with the target status */observerState = state; System. out. println ("the observer's status is:" + state );}}
Client. java
Package com. wsds. test; public class Client {public static void main (String [] args) {// create a topic object ConcreteSubject subject = new ConcreteSubject (); // create the Observer object observer Observer = new ConcreteObserver (); // register the observer object to the topic object subject. attach (observer); // change the status of the topic object subject. change ("new State ");}}
The observer mode can be divided into the push model and pull model. The above is the push mode: the PUSH model: the topic object pushes the details of the topic to the observer, regardless of whether the observer needs it or not, the pushed information is usually all or part of the data of the topic object. Pull model: When a topic object notifies the observer, it only transmits a small amount of information. If the observer needs more specific information, the observer takes the initiative to get it from the topic object, which is equivalent to pulling data from the topic object. In general, the implementation of this model will pass the topic object itself to the observer through the update () method, so that the observer can obtain the data through this reference when it needs to obtain the data. After the model is modified, the Code is as follows:
Package com. wsds. test; import java. util. arrayList; import java. util. list;/*** abstract topic class * @ author Administrator **/public abstract class Subject {// used to save the private List of objects registered with the observer
 
  
List = new ArrayList
  
   
();/*** Register the observer object * @ param Observer observer object */public void attach (observer Observer) {list. add (observer); System. out. println ("Attached an obsever");}/*** Delete the observer object * @ param Observer observer object */public void detach (observer Observer) {list. remove (observer);} public void policyobservers (String newState) {for (Observer observer: list) {observer. update (this );}}}
  
 
Package com. wsds. test;/*** Subject-specific corner class * @ author Administrator **/public class ConcreteSubject extends Subject {private String state; public String getState () {return state ;} public void change (String newState) {state = newState; System. out. println ("the topic status is:" + state); // The status changes and each observer is notified of this. yyobservers (state );}}
Package com. wsds. test; public interface Observer {/*** update interface * @ param state update status */public void update (Subject subject );}
Package com. wsds. test; public class ConcreteObserver implements Observer {// private String observerState of the Observer State; @ Overridepublic void update (Subject subject) {/** update the Observer State, make it consistent with the target status */observerState = (ConcreteSubject) subject ). getState (); System. out. println ("the observer's status is:" + observerState );}}
Comparison of the two modes: the PUSH model assumes that the topic object knows the data that the observer needs, and the PULL model assumes that the topic object does not know the specific data that the observer needs. There is no way to do this, simply pass itself to the observer so that the observer can take the values as needed. The push model may make it difficult to reuse the observer object because the update () method of the observer is a parameter defined as needed and may not take into account the usage that is not taken into account. This means that when a new situation occurs, a new update () method may be provided, or the observer may be re-implemented. Pulling the model won't cause this situation, because pulling the model, the parameter of the update () method is the topic object itself, which is basically the largest set of data that can be transferred by the topic object and can basically meet the needs of various situations.
The Observer mode is supported in Java. The Observer interface and the Observable interface: Observer interface: this interface only defines one method, that is, the update () method, when the State of the object to be observed changes, the notifyObservers () method of the object to be observed calls this method.
Observable interface: the Observable class is a subclass of the java. util. Observable class. Java. util. Observable provides public methods to support observer objects. Two of these methods are very important to the Observable subclass: setChanged () and notifyObservers (). The first method, after setChanged () is called, an internal tag variable is set to indicate that the status of the object to be observed has changed. The second is yyobservers (). When this method is called, the update () method of all registered observer objects is called so that these observer objects can update themselves.
Package com. wsds. test; import java. util. observable;/*** subject-specific corner class * @ author Administrator **/public class ConcreteSubject extends Observable {private String state; public String getState () {return state ;} public void change (String newState) {state = newState; System. out. println ("topic status:" + state); // tag the status change setChanged (); // The status changes, notifying the various observers of this. yyobservers (state );}}
Package com. wsds. test; import java. util. observable; import java. util. observer; public class ConcreteObserver implements Observer {// Observer status private String observerState; public ConcreteObserver (Observable o) {o. addObserver (this) ;}@ Overridepublic void update (Observable o, Object arg) {/** update the Observer State to make it consistent with the target State */observerState = (ConcreteSubject) o ). getState (); System. out. println ("the observer's status is:" + observerState );}}




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.