Design mode-Combination mode

Source: Internet
Author: User
Tags reserved

The English version of the combination mode is: Compose objects into the tree structures to represent part-whole hiearachies. Composite lets clients treat individual objects and compositions of objects uniformly. This means that the object is grouped into a tree structure to represent a "partial-whole" hierarchy, making the user consistent in the use of individual objects and grouped objects.

The combo mode has three roles:

Abstract widget (component) Role: Defines the common methods and properties that participate in a grouped object.

Leaf widget (leaf) role: The role is a leaf object with no branches under it, that is, no child nodes.

Digital widget (Composite) Role: The role represents a grouped, branched, branch object that combines branches and leaves into a tree structure and defines methods for managing child objects.

Combination Mode class Diagram:


Implementation code corresponding to each class:

Code for Abstract Artifacts:

Package com.zz.composite;
/**
 * Abstract Component Interface
 * Copyright April 25, 2015
 * created by TXXS * All right reserved/public
interface Co mponent {public
	void operation ();
}

Branch Structure Code:

Package com.zz.composite;

Import java.util.ArrayList;

/**
 * Defines the branch widget
 * Copyright April 25, 2015
 * created by TXXS * All right reserved/public
class Compos ITE implements Component {

	//widget container
	private arraylist<component> componentlist = new arraylist< Component> ();
	Add widget
	public  void Add (Component Component) {
		this.componentList.add (Component);
	}
	Delete widget public
	void Remove (Component Component) {
		this.componentList.remove (Component);
	}
	Gets the child widget public
	Arraylist<component>getchild () {return
		this.getchild ();
	}
	@Override public
	void operation () {
		//Business logic code
	}

}

Leaf Structure Code:

Package com.zz.composite;
/**
 * Definition leaf widget
 * Copyright April 25, 2015
 * created by TXXS
 * All right reserved/public
class Leaf implements Component {

	@Override public
	void operation () {
		//Salesman logical Code
	}

}

Advantages of Combinatorial mode:

1, the call is simple, the user does not have to care whether they are dealing with a single object or a combination of objects.

2, flexible add nodes, want to increase the branch or leaf node as long as you need to find the parent node, you do not have to add the object parts and modify the code

Combination mode for the scenario:

1. Describe the part of the object and the hierarchical structure of the whole.

2. When you need to ignore the difference between individual parts and composite components

SOURCE download

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.