Android design mode series (1)--SDK source combination 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:

public class view{        //...        Omitted an unrelated 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 (1)--SDK source combination 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.