Design Pattern Learning Summary--Observer pattern

Source: Internet
Author: User

The observer pattern separates the Observer (subscription) from the Observer (advertisement), implements the loose coupling of the code, and defines a one-to-many dependency, allowing multiple observer colleagues to listen to an observer, and at the same time an observer can notify multiple observers of the update.


Role:

Abstract subject (Subject): Also called abstract-observer (Observable). Defines an interface that can add and remove observer objects.

Abstract Observer (Observer): Define an excuse for all the detailed observers to do the update operation.

Detailed topic (Concrete Subject): Implements an abstract topic interface that saves a reference to a list of all observer objects.

When the state changes, the observer in the notification list. Also known as detailed by the observer.

Detailed observer (concrete Observer): Implements an abstract observer interface, which is implemented in more detail by each detailed observer.

Android in development. Contentobserver,setonclicklistener. As well as your own defined interfaces (implements in activity, incoming to asynchronous tasks, when the task is finished, calling the interface function to refresh the activity page) are all very good examples of observers.


While Java itself provides support for the observer pattern, the Java.util library provides examples of the following two classes:


Observer interface, as an abstract observer. Only one interface method update () is defined, and each detailed observer is implemented separately.
The observable class, capable of being either an abstract observer or a detailed observer. Because of this class rather than the interface, the internal implementation has been able to meet the requirements of the detailed observer. Of course, you can inherit and extend this class. The observable class provides two important methods: Setchanged (), which is used to set the internal flag bit, indicating that the observed person has changed. Notifyobservers (), which is used to invoke the update () method for all observers in the list. The Observer does the update operation.


References: http://www.blogjava.net/supercrsky/articles/202544.html

Design Pattern Learning Summary--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.