[Design pattern-structural type] combination (composite)

Source: Internet
Author: User
Summary

Name

Composite

Structure

Motivation

Combine objects into a tree structure to represent? Part-whole? . C o m p o s I t e makes the use of a single object and a composite object consistent.

Applicability

  • The part of the object-the overall hierarchy.

  • You want to ignore the differences between a composite object and a single object. You will use all objects in the composite structure in a unified manner.

Analysis

Image analogy:

Composite-Mary has a birthday today. "You want to give me a gift for my birthday ." "Well, all right, go to the store and pick it for yourself ." "This T-shirt is very beautiful. Buy it. This dress looks good. Buy it. This bag is also good. Buy it ." "Hey, I bought three items. I only promised to give one gift ." "What, a T-shirt with a skirt and a bag, just like a set, miss, please pack it ." "……", Mm will all use the composite mode. Will you?

Merging mode: the merging mode organizes objects into the tree structure and can be used to describe the relationship between the whole and the part. The merging mode is a tree structure mode for processing objects. The merging mode shows the relationship between the part and the whole in a tree structure. The merging mode allows the client to view individual component objects in the same way as the compositing objects composed of them.

1. Component
Declares interfaces for objects in a combination.
When appropriate, implement the default behavior of all class interfaces.
Declare an interface to access and manage child components of component.
(Optional) define an interface in a recursive structure to access a parent component and implement it in combination.

2. Leaf
Indicates the leaf node object in the combination. The leaf node does not have any subnodes.
Defines the behavior of node objects in a combination.

3. Composite
Define the behavior of some parts with child parts.
Storage Sub-parts.
The component interface implements * operations with sub-parts.

4. Client
The component object is manipulated through the component interface.

Instance

For example, a product may consist of multiple products (parts.

The purpose of this example is that a software product can be composed of multiple software products or components.

The files involved include:

Product. Java -- Abstract parent class

-- Softwareproduct. Java

-- Productpart. Java

Testmain. Java

/**    * @author oscar999    * @date 2013-7-25 * @version V1.0    */package designptn.composite;import java.util.List;public abstract class Product {private String name;/** * @return the name */public String getName() {return name;}/** * @param name *            the name to set */public void setName(String name) {this.name = name;}public abstract void add(Product product);public abstract void delete(Product product);public List<Product> products;public void printInfo() {System.out.println(name);}public List<Product> getProducts() {return this.products;}}

/**   * @author oscar999   * @date 2013-7-25* @version V1.0   */package designptn.composite;import java.util.ArrayList;public class SoftwareProduct extends Product {public SoftwareProduct(String name){setName(name);this.products = new ArrayList<Product>();}@Overridepublic void add(Product product) {// TODO Auto-generated method stubthis.products.add(product);}@Overridepublic void delete(Product product) {// TODO Auto-generated method stubthis.products.remove(product);}}

/**    * @author oscar999    * @date 2013-7-25 * @version V1.0    */package designptn.composite;public class ProductPart extends Product {public ProductPart(String name) {setName(name);this.products = null;}@Overridepublic void add(Product product) {// TODO Auto-generated method stub}@Overridepublic void delete(Product product) {// TODO Auto-generated method stub}}

/**    * @author oscar999    * @date 2013-7-25 * @version V1.0    */package designptn.composite;import java.util.List;public class TestMain {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubProduct product = new SoftwareProduct("Software Product");Product subProduct = new SoftwareProduct("Sub Software Product");Product part1 = new ProductPart("Part 1");Product part2 = new ProductPart("Part 2");product.add(subProduct);product.add(part1);product.add(part2);List<Product> products = product.getProducts();for(Product productTemp : products){System.out.println(productTemp.getName());}}}

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.