Mode observer design pattern ||

  • The definition of the observer pattern

Observer pattern name suggests, it is a has monitored objects and monitor objects two roles, when listening stateful objects is changed, all objects will monitor it to make its own response, which is the observer mode. In my understanding, this publish and subscribe model also has a certain similarity.

  • Code Display

First, let's simulate a scenario, the life of the offender as an object being monitored, and the police as a listener can listen criminals. When criminals make criminal acts, it will trigger an update event, you can notify the police to respond to (of course, in reality, is a passive notification). Here, we can code to simulate this scenario.

 

We create a new criminal class that inherits jdk provide Observable class, which is used to represent a monitored objects . The core here is to change the way the state: setChanged (); this is indicated by the observation class if there is a status update, if there is no status change, perform the following notification method will be invalid. Notification Method: notifyObservers (); where internal Vector collection is stored in all observers, it will call all observers of the update () method.

. 1  Package com.hill.observer;
 2  
. 3  Import java.util.Observable;
 . 4  
. 5  / ** 
. 6  * @author HILL
 . 7  * @version V1.0
 . 8  * @date 2019/7/21
 . 9  * a criminal, each offenders will be spying on the police uncle, once the offender is present something to make crimes,
 10  * will be found, eventually was arrested.
. 11  * * / 
12 is  public  class Criminal the extends Observable {
 13 is  
14      public String name;
 15  
16      public Criminal (String name) {
 . 17         the this .name = name;
 18 is      }
 . 19  
20 is      public  void Rob () {
 21 is          System.out.println (name + "ready to rob" );
 22          // parent Observable method, the object changed status flag 
23 is          the setChanged () ;
 24          // notify all police uncle, can be arrested. 
25          the notifyObservers ();
 26 is      }
 27  
28 }

 

Similarly, we create a new police class, use jdk comes Observer interface, this interface update () method is used to indicate when the object of observation status updates, the observer needs to policy enforcement.

. 1  Package com.hill.observer;
 2  
. 3  Import java.util.Observable;
 . 4  Import java.util.Observer;
 . 5  
. 6  / ** 
. 7  * @author HILL
 . 8  * @version V1.0
 . 9  * @date 2019 /. 7 / 21 is
 10  * * / 
. 11  public  class police the implements the Observer {
 12 is  
13 is      @Override
 14      public  void Update (O Observable, Object Arg) {
 15          // policeman aware of crime offenders embodiment measures 
16         Criminal = Criminal (criminal) O;
 . 17          System.out.println ( "police" + criminal.name + "hunt" );
 18      }
 19 }

 

The final Demo is this.

 1 package com.hill.observer;
 2 
 3 import sun.plugin2.message.JavaScriptBaseMessage;
 4 
 5 /**
 6  * @author HILL
 7  * @version V1.0
 8  * @date 2019/7/21
 9  **/
10 public class ObserverDemo {
11 
12     public static void main(String[] args) {
13         Criminal criminal1 = new Criminal("罪犯1");
14         Criminal criminal2 = newCriminal ( "criminal 2" );
 15          Police Police = new new Police ();
 16          // criminals police surveillance 
. 17          criminal1.addObserver (Police);
 18 is          criminal2.addObserver (Police);
 . 19          
20 is          criminal1.rob ();
 21 is          criminal2 .rob ();
 22 is  
23 is      }
 24 }

 

 

  • to sum up

The observer pattern is a concept easy to understand, in practical applications, java become the Swing, web development in the listener (listeners) have used this design pattern. While specific implementations may vary, but several of which it is the same:

  1. The observed
  2. One or more viewers
  3. A collection of records observer
  4. Response method and the notification method

 

Guess you like

Origin www.cnblogs.com/hill1126/p/11234452.html