Design Pattern _ Observer Pattern

Source: Internet
Author: User

Design Pattern _ Observer Pattern

Observer Pattern Define a ont-to-assign dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. (define a one-to-many dependency between objects so that every time an object changes its state, all objects dependent on it will be notified and automatically updated)
● The Subject is defined by the observer as the duty that the observer must perform. It must be able to dynamically add or remove the observer. It is generally an abstract class or an implementation class. It only serves as the duty that must be implemented by the observer: managing the observer and notifying the observer.
● After the Observer receives the message, it performs the update (update method) operation and processes the received information.

Public abstract class Subject {// defines an observer array private Vector
 
  
ObsVector = new Vector
  
   
(); // Add an Observer public void addObserver (Observer o) {this. obsVector. add (o) ;}// delete an Observer public void delObserver (Observer o) {this. obsVector. remove (o);} // notify all observers of public void policyobservers () {for (Observer o: this. obsVector) {o. update ();}}}
  
 

The example monitors the actions of Qin Shihuang, Confucianism, mojia, and legalists.
public interface Observer { public void update(String context);}

/*** This interface is used to manage the Observer in a unified manner * @ author suibian **/public interface ObserverManage {public void addObserver (observer Observer ); public void deleteObserver (Observer observer); public void yyobservers (String context );}

/*** Observer, all of which have * @ author suibian **/public interface IEmperorQin {public void haveLove (); public void haveUnited ();}

Public class EmperorQin implements ObserverManage, IEmperorQin {private ArrayList
 
  
ObserverList = new ArrayList
  
   
();/* Private Vector
   
    
ObserverList = new ArrayList
    
     
(); Thread security */public void addObserver (Observer observer) {this. observerList. add (observer);} public void deleteObserver (Observer observer) {this. observerList. remove (observer);} public void policyobservers (String context) {for (Observer ob: observerList) {ob. update (context) ;}} public void haveLove () {System. out. println (Qin Shihuang started to repair the ahouse Palace); this. notifyObservers (Qin Shihuang started to repair the housing Palace);} public void haveUnited () {System. out. println (Qin Shihuang has already unified the world); this. notifyObservers (Qin Shihuang has already unified the world );}}
    
   
  
 

Public class RuJia implements Observer {public void update (String context) {System. out. println (Confucian: see the LITH activity and prepare for cooperation); this. cry (context); System. out. println (good combination of Confucianism);} private void cry (String context) {System. out. println (the Confucian Foundation-"+ context + formulated the plan );}}

Public class MoJia implements Observer {public void update (String context) {System. out. println. cry (context); System. out. println ();} private void cry (String context) {System. out. println (Mo Jia said, because-"+ context + so I must act immediately );}}

Public class FaJia implements Observer {public void update (String context) {System. out. println (legalist: Observed Qin Shihuang activity); this. reportToQinShiHuang (context); System. out. println ();} private void reportToQinShiHuang (String context) {System. out. println (legalist: Qin Shihuang has an activity-"+ context );}}

Public class QinDynasty {public static void main (String [] args) {FaJia liSi = new FaJia (); MoJia wanSi = new MoJia (); RuJia rujia = new RuJia (); emperorQin qinshihuang = new EmperorQin (); qinshihuang. addObserver (liSi); qinshihuang. addObserver (wanSi); qinshihuang. addObserver (rujia); qinshihuang. haveLove (); qinshihuang. haveUnited () ;}}/* If this is the case, the subscription mode is true, provided that the observer needs to be added to the queue of the observer. When the observer acts, the observer loops through the queue, the reminder methods in the call queue are called one by one. That is to say, the observer must know the internal structure of the observer, or the two of them have reached a protocol with poor scalability. The code structure is clear */

Advantages: The Observer and the observer form a trigger chain through abstract coupling trigger mechanism.
Disadvantage if an observer gets stuck...
Multi-level cross-system message exchange triggered by scenario Association
Java already has a java. util. Observer interface and java. util. Observable interface. It is very easy to use these two interfaces.
 

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.