Detailed explanation of visitor mode of Android programming design mode

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

1、 Introduction

Visitor pattern is a design pattern that separates data operation from data structure. It is the most complex of the 23 design patterns in design patterns, but its frequency of use is not high. As GOF, the author of design patterns, describes visitor pattern: in most cases, you do not need to use visitor pattern, but once you need to use it, Then you really need it.

The basic idea of visitor pattern is that the software system has a relatively stable object structure composed of many objects, and the classes of these objects have an accept method to accept the access of visitor objects. A visitor is an interface that has a visit method that handles different types of elements in the accessed object structure. During one access to the object structure, we traverse the entire object structure and implement the accept method for each element. In the accept method of each element, the visitor's visit method will be called, so that the visitor can handle each element of the object structure. We can design different visitor classes for the object structure to complete different operations, Achieve the effect of differential treatment.

2、 Definition

Encapsulates some operations that act on each element in a data structure. It can define new operations that act on these elements without changing the data structure.

3、 Usage scenario

The object structure is relatively stable, but it is often necessary to define new operations on this object structure.

Many different and irrelevant operations need to be performed on the objects in an object structure. These operations need to avoid "polluting" the classes of these objects, and they do not want to modify these classes when adding new operations.

4、 UML class diagram of visitor pattern

UML class diagram:

Role introduction:

Visitor: an interface or abstract class that defines the access behavior of each element. Parameters are accessible elements. Theoretically, the number of methods is the same as the number of elements. Therefore, the visitor mode requires the structure of the accessed object to be stable. Frequent addition and deletion of elements will inevitably lead to frequent modification of the visitor interface, so the visitor mode is not suitable for use.

Concretevisitor: a specific visitor, which defines the specific access behavior to each element.

Element: an abstract element interface or abstract class that defines a method for receiving visitors so that each element can be accessed by visitors.

Elementa, elementb: specific element classes that provide specific implementations of receive access methods. This concrete implementation usually calls the method provided by the visitor to access the element.

Objectstructure: defines the object structure, which maintains a collection of elements, and iterates these elements for visitors to access.

5、 Simple example

Scenario: at the end of the year, the company will conduct performance appraisal for employees. However, managers in different fields have different evaluation standards for employees. Now the employees include engineers and managers, and the assessors include CEO and CTO. We assume that CTO only focuses on the code quantity of engineers and the number of new products of managers, while CEO focuses on the KPI of engineers and the KPI of managers and the number of new products.

Employee base class:

engineer:

Manager:

Visitor class:

CEO visitor:

CTO visitors:

Employee report:

Client access:

result:

As can be seen from the above code, if you want to add a visitor, you can create a new class that implements the visitor interface, and then implement two visit methods to perform different operations on different elements, so as to achieve the effect of separating data objects from data operations. If you do not use visitor mode and want to perform different operations on different elements, you must use if else and type conversion, which makes it difficult to upgrade and maintain the code.

6、 Visitor mode in Android

The famous open source libraries butterknife, dagger and retrofit in Android are all implemented based on apt (annotation processing tools). The compilation annotation core relies on apt. When we process annotations through apt, we will eventually convert the obtained elements into corresponding element elements to obtain their corresponding information. The source code of the element base class is as follows: (path: javax. Lang.model. Element. Element)

Elementvisitor is the visitor type. The source code of elementvisitor is as follows:

In elementvisitor, multiple visit interfaces are defined, and each interface handles one element type. This is a typical visitor pattern.

7、 Summary

As GOF is quoted at the beginning of this section: in most cases, you don't need to use visitor mode, but once you need to use it, you really need it. In reality, we should evaluate whether the visitor pattern is suitable according to the specific situation. For example, whether our object structure is stable enough and whether using the visitor pattern can optimize our code rather than make our code more complex. Before using a pattern, we should clarify its use scenario and what problems it can solve, so as to avoid abusing design patterns.

advantage:

The separation of roles and responsibilities conforms to the principle of single responsibility.

Excellent scalability.

The data structure is decoupled from the operations acting on the structure, so that the operation set can change independently.

Flexibility.

Disadvantages:

The specific elements publish details to visitors, which violates the Dimitri principle.

When the specific element is changed, the modification cost is large.

In violation of the dependency inversion principle, in order to achieve "differential treatment", it relies on specific classes without relying on abstraction.

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