The application of the combination mode in Android is a flood of porridge, everywhere, and that is the use of view and ViewGroup classes. In the Android UI design, almost all of the widget and layout classes depend on these two classes.
Combination mode, composite pattern, is a very ingenious model. Almost all object-oriented systems are applied to the combination mode.
1. Intent
Combine object view and viewgroup into a tree structure to represent a "partial-whole" hierarchy (view can be part of a viewgroup).
The combination mode makes the user consistent in the use of a single object view and a composite object viewgroup.
Hot words: Part-whole container-content tree structure consistent leaf synthesis security transparency
2. Structure
For view and ViewGroup, we choose the safe combination mode (add Add,remove,getchild method to the composite object), add a little comment, we modify the above to:
3. Code
Implementation of the View class:
public class view{
//
... Omitted an unrelated method
}
ViewGroup implementation: Public
abstract class ViewGroup extends view{
/** * Adds
a child view.< c8/>*/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. Effect
(1). Structural pattern
(2). Defines a class hierarchy that contains Basic objects and grouped objects. This structure can control the use of basic objects and composite objects flexibly.
(3). Simplify customer code. Basic objects and grouped 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.
The above is a small set to introduce the Android design Model series of the combination mode, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!