Android Design Pattern series (1)-SDK source code combination pattern, android-sdk

Source: Internet
Author: User

Android Design Pattern series (1)-SDK source code combination pattern, android-sdk

The application of the combination mode in Android is everywhere, that is, the use of the View and ViewGroup classes. In android UI design, almost all widgets and layout classes depend on these two classes.
Composite Pattern is a very clever mode. Almost all object-oriented systems are applied to the combination mode.

1. Intention
Combine the object View and ViewGroup into a tree structure to represent the "part-whole" hierarchy (View can be used as part of the ViewGroup ).
The combination mode ensures consistency between the use of a single object View and a composite object ViewGroup.
Hot words:Part-whole Container-content tree structure consistency leaf synthesis security transparency

2. Structure

For the actual situation of View and ViewGroup, we select a safe combination mode (add, remove, and getChild methods to the combination object), add a few comments, and modify it:

3. Code
View class implementation:

Public class View {// ...... // irrelevant methods are omitted}

ViewGroup implementation:

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. Results
(1). Structural Mode
(2) defines a class hierarchy that contains basic objects and composite objects. This structure can flexibly control the use of basic objects and composite objects.
(3). Simplify customer code. The basic objects and composite objects are consistent, so you do not need to distinguish them.
(4) makes it easier to add new types of components.
(5) Make your design more general.


Is the android source code package different from the android SDK development?

Android source code development is generally used by mobile phone manufacturers, because it involves modifying the source code and designing a UI suitable for your mobile phone.
Android SDK is used to develop its own applications based on the standard android sdk.
Pai_^

In Java, there are 23 design patterns. Which of the following are used in Android?

The so-called mode is actually a common idea. After you get familiar with it, you won't even consider what it is. There are more than 23 design patterns.
 

Related Article

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.