"Design pattern" Java design pattern the first lesson of the observer pattern

Source: Internet
Author: User

We can use a situation in our life to get to know the observer pattern.


In this age, it is possible that the old people in our family still keep the habit of reading newspapers every day. The newspaper is responsible for producing the content, organizing it into a newspaper, printing it every day, and then sending it to every user who has paid for the newspaper. So that the old man can look at what happens every day.


This is the most appropriate metaphor for the observer pattern. Subscribers and newspapers are a one-to-many relationship, every day as long as the content is updated, will be unified to send the newspaper to the user's hand, of course, if you do not want to subscribe, you can unsubscribe. The newspaper doesn't know your status and doesn't care if you read the newspaper. It is the duty to send the newspaper to your house on time and every day.


Here is a summary of some of the key points about the observer pattern when I read a book:

    • Design principle: Strive for loose coupling between interacting objects.
    • Defines a one-to-many dependency between objects, so that when an object changes, all its dependents are notified and updated automatically.
    • The Observer pattern provides an object design that allows for loose coupling between the observer and the Observer.
    • An observer is stateful.
    • Observers can update the observer with a common interface
    • Observers do not know the details of the observer, only that the observer implements the Observer interface
    • The loosely coupled design allows us to build resilient OO systems that can cope with change because the interdependence between objects is minimized.
    • Java has a built-in observer pattern, the observer interface and the observable class in the Java.util package, and you can use push and pull two ways
    • Be aware of the limitations of the built-in observer pattern in Java, because Observer is a class, not an interface
    • The MVC pattern is the best representation of the Observer pattern
    • The observer pattern is also applied to the Javabeans,rmi.



"Design pattern" Java design pattern the first lesson of the observer pattern

Related Article

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.