Android in the combination of the application of the model is flooded into porridge, everywhere, that is the use of the view and ViewGroup class. In the Android UI design, almost all widgets and layout classes rely on these two classes.
The combination mode, Composite pattern, is a very ingenious pattern. Almost all object-oriented systems are applied to the combined mode.
1. Intentions
Combines object view and viewgroup into a tree structure to represent a "partial-whole" hierarchy (view can be a part of ViewGroup).
The combined mode makes the user consistent with the use of single object view and composite object ViewGroup.
Hot words: Part-Overall container-content tree structure consistency leaf synthesis security transparency
2. Structure
For the actual situation of view and viewgroup, we choose the safe combination mode (add Add,remove,getchild method in the Combined object), add a little comment, we change to:
3. Code
Implementation of the View class:
- public class view{
- //... ...
- Omitted the irrelevant method.
- }
Implementation of ViewGroup:
- Public abstract class ViewGroup extends view{
- /**
- * Adds a child view.
- */
- public void AddView (View child) {
- //...
- }
- public void Removeview (view view) {
- //...
- }
- /**
- * Returns The view at the specified position in the group.
- */
- Public View getchildat (int index) {
- try {
- return Mchildren[index];
- } catch (Indexoutofboundsexception ex) {
- return null;
- }
- }
- Other methods
- }
4. Effects
(1). Structural mode
(2). Defines a class hierarchy that contains Basic objects and composite objects. This structure provides the flexibility to control the use of basic objects and composite objects.
(3). Simplify the customer code. Basic objects and composite objects are consistent, and users do not have to differentiate between them.
(4). Makes it easier to add new types of components.
(5). Make your design more generalized.
Android design mode series-combined mode