Combination mode of Android design mode series

The application of composite mode in Android can be seen everywhere, that is, the use of view and ViewGroup classes. In Android UI design, almost all widget and layout classes rely on these two classes.

Composite pattern is a very clever pattern. Almost all object-oriented systems are applied to composite patterns.

1. Intention

Combine the object view and ViewGroup into a tree structure to represent the "part whole" hierarchy (view can be part of ViewGroup).

The combined mode enables users to use the single object view and the combined object ViewGroup consistently.

Hot words: part - whole container - content tree structure consistency leaf composition security transparency

2. Structure

According to the actual situation of view and ViewGroup, we choose the safe combination mode (add, remove and getchild methods in the combination object) and add a few comments. We modify the above figure as follows:

3. Code

Implementation of view class:

4. Effect

(1). Structural model

(2). Defines a class hierarchy containing basic objects and composite objects. This structure can flexibly control the use of basic objects and composite objects.

(3). Simplify customer code. Basic objects and composite objects are consistent, and users do not need to distinguish them.

(4). Makes it easier to add new types of components.

(5). Make your design more general.

The above is the combination mode of Android design mode series introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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