Design mode 13-Observer (behavior)

Source: Internet
Author: User
1. Scene mode Abstraction

How do people who subscribe to newspapers know if they are not sure about the time when the newspaper comes? It can be abstracted as: when the state of an object changes, how can we notify and process all objects dependent on it? The most common example in your life is that when you get online, the QQ background will give you all the messages you receive when you are not online.
2. Use observer mode for solution 2.1 observer mode definition

Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and updated automatically.
2.2 observer mode structure description

 
2. 3. Sample Code of the observer Mode

Package demo11.observer. example1; import Java. util. *;/*** indicates the target object, which knows the observer who observes it, and provides the interface */public class subject {/*** for registering and deleting the observer to save the registered observer object */private list <observer> Observers = new arraylist <observer> (); /*** register the observer object * @ Param observer object */Public void attach (Observer observer) {observers. add (observer);}/*** Delete observer object * @ Param observer object */Public void detach (Observer observer) {observers. remove (observer);}/*** notify all registered observer objects */protected void policyobservers () {for (Observer observer: Observers) {observer. update (this );}}} **************************************** **************************************** * ****************** package demo11.observer. example1;/*** specific target object, responsible for storing the relevant State to the corresponding observer object, * and when the state changes, notify each Observer */public class concretesubject extends subject {/*** to indicate the status of the target object */private string subjectstate; Public String getsubjectstate () {return subjectstate ;} public void setsubjectstate (string subjectstate) {This. subjectstate = subjectstate; // The status changes and each observer is notified of this. yyobservers ();}} **************************************** **************************************** * ****************** package demo11.observer. example1;/*** observer interface, define an updated interface for objects notified when the target changes */public interface observer {/*** updated interface * @ Param subject to pass in the target object, obtain the status of the target object */Public void Update (subject );} **************************************** **************************************** * ****************** package demo11.observer. example1;/*** the updated method of the specific observer object to keep its status consistent with that of the target */public class concreteobserver implements observer, viewer's status */private string observerstate; Public void Update (subject) {// specific update Implementation // you may need to update the observer's status here, make it consistent with the target State. observerstate = (concretesubject) subject ). getsubjectstate ();}}
3. Use the observer mode to implement the target object in example 3.1 as the observer
Package demo11.observer. example2; import Java. util. arraylist; import Java. util. list;/*** target object, used as the Observer */public class subject {/*** to save the registered observer object, that is, the newspaper subscriber */private list <observer> readers = new arraylist <observer> ();/*** readers of newspapers must subscribe to newspapers first, register * @ Param reader * @ return whether the newspaper is successfully registered */Public void attach (Observer reader) {readers. add (Reader);}/*** readers of a newspaper can cancel * @ Param reader * @ return: whether the newspaper is successfully canceled */Public void detach (Observer reader) {readers. remove (Reader);}/*** when each newspaper is printed, it must be quickly and automatically delivered to the reader. * It is equivalent to notifying the reader, let them know */protected void policyobservers () {for (Observer reader: readers) {reader. update (this );}}}
3.2 target newspaper objects
Package demo11.observer. example2;/*** newspaper object, specific goals */public class newspaper extends subject {/*** specific newspaper content */private string content; /*** get the specific newspaper content ** @ return the specific newspaper content */Public String getcontent () {return content, set the specific content of a newspaper, which is equivalent to the specific content of the ** @ Param content */Public void setcontent (string content) {This. content = content; // If the content already exists and the description is published again, notify all readers of policyobservers ();}}
3.3 observers, such as readers of newspapers
Package demo11.observer. example2;/*** observer, such as newspaper reader */public interface observer {/*** method of notification * @ Param subject specific target object, you can obtain the newspaper content */Public void Update (subject );}
3.4 true readers can briefly describe their names
Package demo11.observer. example2;/*** real reader, to briefly describe the name */public class reader implements observer {/*** reader's name */private string name; public void Update (subject) {// This is a pull method system. out. println (name + "received the newspaper. Read it first. Content = "+ (newspaper) subject ). getcontent ();} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}
3.5 client usage
Package demo11.observer. example2; public class client {public static void main (string [] ARGs) {// create a newspaper as the Observer newspaper subject = new newspaper (); // create reader, that is, the observer reader reader1 = new reader (); reader1.setname ("Zhang San"); reader reader2 = new reader (); reader2.setname ("Li Si "); reader reader3 = new reader (); reader3.setname (""); // register the reader subject. attach (reader1); Subject. attach (reader2); Subject. attach (reader3); // publish the newspaper subject. setcontent ("the current content is the observer mode ");}}
4. understanding the relationship between the observer and the target in observer mode 4.1

A typical one-to-many relationship.
4.2 unidirectional dependency

The observer depends on the target, and the target does not depend on the observer.
4.3 Call Sequence diagram of observer Mode

 
4.4 PUSH model

Push model: As the name implies, the target object actively pushes messages to the observer, regardless of whether the observer needs them or not, which is equivalent to broadcast communication.
What is different from the previous one is that

Public void Update (string content) {// This is the push method system. Out. println (name + ". Read the newspaper first. Content = "+ content);}/*** when each newspaper is printed, it is necessary to promptly take the initiative to be delivered to the reader. * It is equivalent to notifying the reader, let them know the content * @ Param content to be actively pushed */protected void policyobservers (string content) {for (Observer reader: readers) {reader. update (content) ;}}/*** to set the specific newspaper content, it is equivalent to publishing the specific content of the * @ Param content newspaper */Public void setcontent (string content) {This. content = content; // If the content already exists and the description is published again, notify all readers of policyobservers (content );}
4.5 PULL model

Pull model: As the name implies, a target object only transmits a small amount of information when notifying the observer. If the observer needs more specific information, the observer takes the initiative to obtain the target object.

Public void Update (subject) {// This is the pull method system. Out. println (name + "). Read the newspaper first. Content = "+ (newspaper) subject). getcontent ());}
4.6java observer Mode

The observer mode has been defined in Java. For details, see. I will not repeat it here. Basically.
5. Observer mode thinking 5.1 advantages and disadvantages:

Advantage: Implements abstract coupling between the observer and the target, achieves dynamic interaction, and supports broadcast communication.
Disadvantage: unnecessary operations may occur.
5.2 nature of observer mode:

Trigger Association

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.