18. Combination design mode

Source: Internet
Author: User

1. Introduction to Combinatorial Design patterns

Combinatorial design patterns are more common in everyday life, and in data structures we are tree structures.
Common ones are:
1. Multi-level Tree menu
2. Files and folders directory

Defined

Combine objects into a tree structure to represent a "partial-whole" hierarchy, making the use of objects and composite objects consistent.

2. Combination design Pattern Usage scenarios
    • Represents the part of an object-the overall hierarchy.
    • A scenario in which a part of a template or function can be detached from a whole.
3. Combinatorial design mode UML class

Role description
    • Component: An abstract root node that declares an interface for an object in a composition. When appropriate, implements the default behavior for all classes of common interfaces.

    • Composite: Defines the behavior of those branch nodes that have child nodes, stores child nodes, and implements operations related to sub-nodes in the component interface.

    • Leaf: The leaf node object is represented in the composition, the leaf node has no child nodes, and the behavior of the node object is defined in the composition.

    • Client: Manipulates the object of the combined node through the component interface.

4. Simple implementation of combinatorial design pattern
    • (1), first define an abstract root node:
publicabstractclass Component {    protected String name;    publicComponent(String name) {        this.name = name;    }    /**     * 具体的逻辑方法由子类实现     */    publicabstractvoiddoSomething();}

Abstract root node inside there is an abstract method dosomething (). Specific methods are implemented by subclasses.

    • (2), then define the specific branch node:
 Public  class compsite extends Component {    Privatelist<component> components =NewArraylist<> (); Public Compsite(String name) {Super(name); }@Override     Public void dosomething() {System.out.println (name);if(NULL! = components) { for(Component component:components)            {component.dosomething (); }        }    }/** * Add child nodes * * @param Children * *     Public void AddChild(Component Child)    {Components.add (child); }/** * Removing sub-nodes * * @param Child */     Public void RemoveChild(Component Child)    {Components.remove (child); }/** * Get sub-node corresponding subscript * @param index * @return  */     PublicComponentGetChildren(intIndex) {returnComponents.get (index); }}

In the specific branch node, there is a storage node container, used to store the sub-nodes, in the DoSomething () method, the first print out the name of the current node, and then traverse all the sub-nodes, call its DoSomething method.

At the same time there are also the operation of sub-node set of additions and deletions to find ways to change.

    • (3), then we define the specific leaf node
publicclass Leaf extends Component {    publicLeaf(String name) {        super(name);    }    @Override    publicvoiddoSomething() {        System.out.println(name);    }}

There is no container in the leaf node that stores the sub-nodes, and of course there is no method for adding or deleting the container. The name of the current leaf node is printed directly inside the DoSomething method.

    • (4), test client
 Public classClient { Public Static void Main(string[] args) {//Construction of a root nodeCompsite root =NewCompsite ("Root");//Construction of two branches nodeCompsite branch1 =NewCompsite ("Branch1"); Compsite BRANCH2 =NewCompsite ("BRANCH2");//construction of two leaf nodesLeaf LEAF1 =NewLeaf ("LEAF1"); Leaf leaf2 =NewLeaf ("Leaf2");//Add two leaves to the branch nodeBranch1.addchild (LEAF1); Branch2.addchild (LEAF2);//Add a branch node to the root nodeRoot.addchild (BRANCH1);        Root.addchild (BRANCH2);    Root.dosomething (); }}
5. Combination design mode in Android source code

A typical example of Android source code is the nested combination of view and viewgroup.

In the view-level tree species shown above, view controls TextView, Buttong, and so on inherit from the View,viewgroup container also inherit from view. ViewGroup also includes other view. However, view controls such as TextView cannot contain other controls.

ViewGroup The view operation method with respect to views. Similar to the branch node above the leaf nodes more than the child node operation method.

Why is viewgroup more than view operation method?

Because the ViewGroup implements the Viewparent and Viewmanager interfaces, the Viewmanager interface defines the methods for sub-view operations such as Addview,removeview.

Since our focus is on the view and viewgroup combination patterns, there is too much analysis of the details inside, and the relationship between them can be seen with the above inheritance.

6. Summary

The combinatorial design pattern is not much seen in Android development, we do understand here.

    • Advantages:
      • Combined mode decouples The internal structure of the client and complex elements, allowing the client to handle complex elements like simple elements.

18. Combination design 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.