Detailed explanation of observer mode example of Android programming design mode

This paper illustrates the observer mode of Android programming design mode. Share with you for your reference, as follows:

1、 Introduction

Observer mode is a highly used mode. Its most common places are GUI system and subscription publishing system. Because an important role of this model is decoupling, which decouples the observer and the observer, making them less dependent or even independent. For the GUI system, the UI of the application is changeable, especially in the early stage, with the change of business or the modification of product requirements, the application interface will change frequently, but the business logic basically changes little. At this time, the GUI system needs a set of mechanism to deal with this situation, so as to decouple the UI layer from the specific business logic, and the observer mode will come in handy at this time.

2、 Definition

Define a one to many dependency between objects, so that whenever an object changes state, all objects that depend on it will be notified and updated automatically.

3、 Usage scenario

For the associated behavior scenario, it should be noted that the associated behavior is separable, not a "combination" relationship.

Event multi-level trigger scenario.

Cross system message exchange scenarios, such as message queue and event bus processing mechanism.

4、 UML class diagram of observer pattern

UML class diagram:

Role introduction:

Subject: abstract topic, that is, the role of the observable. The abstract topic role saves the references of all observer objects in a cluster, and each topic can have any number of observers. Abstract topics provide an interface to add and delete observer objects.

Concretesubject: a specific topic. This role stores the relevant status into the specific observer object. When the internal status of a specific topic changes, it will send a notice to all registered observers. The specific topic role is also called the concrete observable role.

Observer: Abstract observer. This role is the abstract class of the observer. It defines an update interface to update itself when notified of the change of the subject.

Concreteobserver: a concrete observer. This role implements the update interface defined by the abstract observer role to update its own state when the state of the topic changes.

5、 Simple implementation

Here is an example of chasing a play. Usually, in order not to miss the latest TV play, we will subscribe to or pay attention to the TV play. When the TV play is updated, it will be pushed to us as soon as possible, and then we will simply implement it.

Abstract Observer class:

Abstract Observer class:

Specific observer classes:

Specific observed categories:

realization:

result:

It can be seen from the above code that one to many message push is implemented. Push messages rely on abstract classes such as observer and observable, while user and teleplay are completely uncoupled, ensuring the flexibility and scalability of the subscription system.

6、 Observer mode in Android source code

1、BaseAdapter

I believe everyone is familiar with baseadapter. We all inherit it in the listview adapter. Here is a simple analysis.

Baseadapter part code:

Look at the mdatasetobservable. Notifychanged() method:

You can see that all observers are traversed in mdatasetobservable. Notifychanged() and their onchanged() is called to tell the observer what happened.

So how does the observer come from? That is, the setadapter method. The code is as follows:

Adapterdatasetobserver is defined in the parent class abslistview of listview. It is a dataset observer. Code:

It consists of adapterdatasetobserver inherited from the parent class AdapterView of abslistview. The code is as follows:

When the listview data changes, call the notifydatasetchanged function of the adapter, which will call the notifychanged function of datasetobservable, which will call the onchanged method of all observers (adapterdatasetobserver). This is an observer mode!

7、 Summary

advantage:

There is abstract coupling between the observer and the observed to cope with business changes.

Enhance the flexibility and scalability of the system.

Disadvantages:

When applying the observer mode, we need to consider the development efficiency and operation efficiency. The program includes one observer and multiple observers. The development and debugging contents will be more complex. Moreover, in Java, the notification of messages is generally executed in sequence. If an observer is stuck, it will affect the overall execution efficiency. In this case, asynchronous implementation is generally adopted.

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
分享
二维码
< <上一篇
下一篇>>