Observer Design Pattern

Source: Internet
Author: User

Observer Design Pattern

Observer mode: defines a one-to-many dependency between objects. When the status of an object changes,
All objects dependent on it are notified and automatically updated.

In this definition, the first keyword is dependency, which is a type of relationship between classes. Dependency refers to dependency between one class and another class, generally, objects are transmitted in the method by using the form parameters. Second, how does one automatically update the observer after the target object is notified? In fact, it is not difficult to do this, that is, the object to which the class has an automatic update method can be automatically updated,


The observer module roles include:

Subject: Target

ConcreteSubject: Target

Observer: Observer

ConcreteObserver: Specific observer


For example, if a cat is a mouse or a sufficient observer, a mouse or a dog is an observer, and a cat is an observer or a target object, the target object is one, but the observer can have more than one, the cat calls the mouse to run, the cat calls the dog to run, and now uses the observer mode to write the code.


Subject. java target object abstract class, of course, can also be written as an interface

/*** The observed targets have a common method for adding and deleting notifications * @ author carpool */public interface Subject {// register or add an Observer void resgister (observer Observer ); // delete the Observer void delete (observer Observer); // The behavior attribute void cry () of the target ();}


Specific target object, Cat. java

Public class Cat implements Subject {/*** Method for registering an observer for an external Department class */List
  
   
List = new ArrayList
   
    
(); @ Overridepublic void resgister (Observer observer) {list. add (observer) ;}@ Overridepublic void delete (Observer observer) {list. remove (observer);}/*** notify the registered observer * the mouse and dog to run */@ Overridepublic void cry () {for (Observer observer: list) {observer. response ();}}}
   
  


Observer. java Observer abstract class

Public interface Observer {void response (); // defines the method for automatically updating the Observer}

Dog. java specific observer

Public class Dog implements Observer {@ Overridepublic void response () {System. out. println ("the cat is calling to notify the Dog to run quickly ...... ");}}

Mouse. java is also one of the observers.

Public class Mouse implements Observer {@ Overridepublic void response () {System. out. println ("the Mouse is notified to run quickly ...... ");}}


Client. java registers the observer and target notification

Public class Client {public static void main (String [] args) {Cat cat = new Cat (); // create the target Dog dog = new Dog (); // create the observer Mouse = new mouse (); // create the observer cat. resgister (mouse); // registers the observer to the target object. Generally, the registered observer cat is saved in a set. resgister (dog); // registers the observer to the target object. Generally, the registered observer cat is saved in a set. cry (); // notifies the target object, which should be changed and then automatically updated by the observer }}


Print result:

The cat calls to inform the mouse to run ...... The cat is calling to inform the dog to run ......


The printed results show that the order of the registered observer object is the same as that of the registered observer object,

The requirement is as follows: when an observer updates a user's behavior, for example, whether the target object is logged on, it is updated only when the target object is logged on, adding such a requirement,

Analysis: based on the preceding example

@ Overridepublic void response () {System. out. println ("the cat calls to notify the dog to run quickly ...... ");}

This is an update method, but to determine whether the login of the target object is successful, you must hold the target object to update it, either by passing the parameter or by using the constructor,

I. Implementation through constructor:

Subject. java

/*** The observed targets have a common method for adding and deleting notifications * @ author carpool */public interface Subject {// register or add an Observer void resgister (observer Observer ); // delete the Observer void delete (observer Observer); // The behavior attribute void cry () of the target ();}


Cat. java

Public class Cat implements Subject {/*** Method for registering an observer for an external Department class */List
  
   
List = new ArrayList
   
    
(); @ Overridepublic void resgister (Observer observer) {list. add (observer) ;}@ Overridepublic void delete (Observer observer) {list. remove (observer);}/*** notify the registered observer * the mouse and dog to run */@ Overridepublic void cry () {for (Observer observer: list) {observer. response () ;}/ *** login * @ return */public boolean login () {return true ;}}
   
  

Observer. java

Public interface Observer {void response (); // defines the method for automatically updating the Observer}


Dog. java

Public class Dog implements Observer {public Cat cat; public Dog (Cat cat) {this. cat = cat ;}@ Overridepublic void response () {if (cat. login () {System. out. println ...... ");}}}

Mouse. java

Public class Mouse implements Observer {public Cat cat; public Mouse (Cat cat) {this. cat = cat ;}@ Overridepublic void response () {if (cat. login () {System. out. println ...... ");}}}


Client. jav

Public class Client {public static void main (String [] args) {Cat cat = new Cat (); // create the target Dog dog = new Dog (cat ); // create an observer Mouse = new mouse (cat); // create an observer cat. resgister (mouse); // registers the observer to the target object. Generally, the registered observer cat is saved in a set. resgister (dog); // registers the observer to the target object. Generally, the registered observer cat is saved in a set. cry (); // notifies the target object, which should be changed and then automatically updated by the observer }}

Print result:

The cat calls to inform the mouse to run ...... The cat is calling to inform the dog to run ......


Second, the transmission mode is as follows:

Public interface Observer {void response (Subject subject); // defines the method for automatically updating the Observer}


However, in jdk, sun actually helped us implement the observer mode. We only need to implement the exposed interfaces,


Advantages of the observer module:

In this way, the presentation layer and the data logic layer can be separated, and an abstract coupling is established between the observation object and the observer to support broadcast communication,


Disadvantage: when there are many observers, it will take a lot of time to notify the observer to update!

















Zookeeper

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.