The combination mode _android of the Android design pattern series

Source: Internet
Author: User

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!

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.