Android design mode series-combined mode

Source: Internet
Author: User

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:

    1. public class view{
    2. //... ...
    3. Omitted the irrelevant method.
    4. }

Implementation of ViewGroup:

  1. Public abstract class ViewGroup extends view{
  2. /**
  3. * Adds a child view.
  4. */
  5. public void AddView (View child) {
  6. //...
  7. }
  8. public void Removeview (view view) {
  9. //...
  10. }
  11. /**
  12. * Returns The view at the specified position in the group.
  13. */
  14. Public View getchildat (int index) {
  15. try {
  16. return Mchildren[index];
  17. } catch (Indexoutofboundsexception ex) {
  18. return null;
  19. }
  20. }
  21. Other methods
  22. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.