Android development implements simple observer and observed examples

This paper describes the simple observer and observed in Android development. Share with you for your reference, as follows:

summary:

Observer mode (also known as publish / subscribe mode) is a kind of behavioral mode. It defines a one to many dependency that allows multiple observer objects to listen to a topic object at the same time. When the status of the topic object changes, it will notify all observer objects so that they can update themselves automatically.

Observer mode structure diagram

Subject: abstract topic (Abstract observer). The abstract topic role saves all observer objects in a collection. Each topic can have any number of observers. The abstract topic provides an interface to add and delete observer objects. Concretesubject: a specific subject (specific observer). This role stores the relevant status in the specific observer object. When the internal status of a specific subject changes, it sends a notice to all registered observers. Observer: Abstract observer is an abstract class of observer. It defines an update interface to update itself when notified of topic change. Concreteobserver: a concrete observer, which implements the update interface defined by the abstract observer to update its own status when notified of the subject change.

Implementation method

1. Create an interface observer (watcher. Java):

2. Create interface watchee (watched. Java):

3. Create observer implementation interface (conwatcher. Java):

4. Create the observer implementation interface (conwatched. Java):

5. Test (watchtest. Java):

File directory structure:

Operation results:

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>