Head First Design pattern reading notes (2) Observer mode

Source: Internet
Author: User
Head First Design pattern reading notes (2) OBSERVER pattern Observer mode Observer pattern class diagram

Definition

Observer patterns: Define a one-to-many relationship between objects, and when one of the objects changes, all objects that depend on the object (that is, the observer) automatically update or perform certain behaviors.

Some of the original test of OO
  1. To deal with inter-object relationships in a loosely coupled manner –> the benefits of "Gaonezu, low coupling" when software engineering is learned
About viewer mode
  1. The observed object notifies the observer that a push can be used (the Notifyaction method with parameters in the class diagram), or it can use the observer's own way of obtaining updates (the Notifyaction method without parameters in the class diagram). The individual feels that the latter is more flexible, because the observed object is only responsible for notifying, without transmitting any data to the observer, and the observer is getting the data according to its own needs at the time of the update, and it can be updated asynchronously. However, the above class diagram is a bit bad, that is, the specific observer class can only inherit the observer abstract class instead of implementing the Observer interface. If you are going to implement an interface, you should change the Abstractobserver class above to an interface and change notifyaction () to Notifyaction (Abstractsubject a).
  2. Many of the GUI framework's event and listener mechanisms are implemented through the observer pattern.
  3. Java has its own observer pattern implementation, using the Java.util.Observer interface and the Java.util.Observable class.
  4. Depending on the implementation, the observed objects are not necessarily in the order of the list, and it is better not to have the logic to rely on this notification order elsewhere.

Head First Design pattern reading notes (2) Observer mode

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.