The pattern of the vegetable design--observer pattern

Source: Internet
Author: User


background

The last time I went to the cinema to see a movie, half the sister said that as long as the sweep can be free to collect popcorn, you say how I hide from the sister's tricks? So pick up my cell phone, sweep, register the membership, OK to collect popcorn. Time passed, I found that my phone is always getting some messy information, once there is any big thing I will receive information, you can know I refused. Well, here's the observer pattern, because my mobile phone number has become the recipient of their message, or the listener, or the observer.


1. Intention to use

The observer pattern seems to be useful when an object changes while another object needs to be changed.


2. Life Example

A meal, all the colleagues including I immediately put down the hands of the work, have to watch the NBA, have to sleep, have to go directly to the cafeteria line and so on food. It seems that there is less a notification process, oh, it doesn't matter.

3. Java Example (framework, JDK, JEE)

Because the nature of the observer is the listener, the most common use of the listener is swing or AWT, such as a button being clicked to trigger the Click event, the mouse movement triggers the mouseover event, and the event is triggered, and the corresponding observer (listener) is notified

4. Pattern class Diagram

  1. Abstract Theme Role ( Subject ): Defines an interface that declares the method that the subject role should have (a method declaration such as additions and deletions to the observer object).

  2. Theme Role ( Concrete Subject ): Implements an abstract theme role, implements all of its methods, and contains a collection reference for the role of an abstract observer as a member variable, meaning that a subject role can have multiple observers.

  3. Abstract Observer Role ( Observer ): Defines an interface that declares the method that the most observer role should have (this method is the response when the observer receives the notification, typically Update ).

  4. specific observer role ( Concrete Observer ): The implementation of the abstract observer role, mainly in the implementation of the interface Update method.


5. Advantages of the Model

Observer pattern: Defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time, and the subject object notifies all observer objects when the state changes, so that they can automatically update themselves. (also called publish-subscribe mode) The work of the observer is actually decoupling, so that both sides of the coupling are dependent on abstraction, not on specifics. So that their changes will not affect the other side of the change.


6. Comparison with similar models

The Observer pattern is one of the most commonly used design patterns, especially when the jquery front end, the registration of events is an embodiment of the observer pattern. The difference between the observer and the other patterns is very wide.




The pattern of the vegetable design--observer pattern

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.