Composite Mode combination Mode

Source: Internet
Author: User

Android ViewGroup and View relationships, that is, the use of a combination mode

1. Overview

In the data structure, the tree structure is very important, we can apply the structure of the tree to the design pattern inside.

Example 1: A multi-level tree menu.

Example 2: Files and folders directory

2. Questions

We can combine complex objects with simple objects, and this complex object can be combined into larger objects. We can define simple objects as classes and then define some container classes to store these simple objects. The client code must distinguish between object simple objects and container objects, but in most cases the user thinks they are the same. The difference between these classes makes the program more complex. Recursion is used with trouble, and how do we use recursive combinations so that users don't have to differentiate between these classes?

3. Solution

composition Mode : Combines objects into a tree structure to represent a "partial-whole" hierarchy. Composite makes the user consistent with the use of individual objects and composite objects.

Sometimes called part-overall mode, it blurs the concept of simple elements and complex elements in the problem of our tree structure, and the client program can deal with complex elements like simple elements, thus decoupling the client program from the internal structure of the complex elements.

The combined mode allows you to optimize the processing of recursive or hierarchical data structures. There are many examples of hierarchical data structures that make combinatorial patterns very useful. A universal example of hierarchical data structures is what you encounter every time you use a computer: the file system. The file system consists of directories and files. Each directory can be loaded with content. The contents of a directory can be either a file or a directory. In this way, the computer's file system is organized in a recursive structure. If you want to describe such a data structure, then you can use the combined mode composite.

4. Classification of combinatorial patterns

1) Define the methods for managing child elements in the composite class
2) Define the methods for managing child elements in the component interface so that the leaf class needs to implement NULL for these methods.

5. Applicability

The composite mode is applicable in the following cases:

1). You want to represent the part of the object-the overall hierarchy

2). You want users to ignore the difference between a combined object and a single object, and the user will use all the objects in the composite structure uniformly.

6. Structure



The typical composite object structure is as follows:

Composite Mode 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.