Combination Mode (Composite)

Source: Internet
Author: User

The combination mode is sometimes called Part-Overall mode is convenient when dealing with a tree-like structure, look at the diagram:

public class treenode {private  string name;private treenode parent;private vector<treenode> children =  new Vector<TreeNode> ();p Ublic treenode (string name) {this.name = name;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public treenode getparent ()  {return parent;} Public void setparent (treenode parent)  {this.parent = parent;} Add child node Public void add (treenode node) {Children.add (node);} Delete child node Public void remove (treenode node) {children.remove (node);} Get child node Public enumeration<treenode> getchildren () {return children.elements ();}} 
public class Tree {TreeNode root = null;public tree (String name) {root = new TreeNode (name);} public static void Main (string[] args) {Tree tree = new Tree ("A"); TreeNode NodeB = new TreeNode ("B"); TreeNode NodeC = new TreeNode ("C"); Nodeb.add (NODEC); Tree.root.add (NodeB); SYSTEM.OUT.PRINTLN ("Build the Tree finished!");}}

Use scene: Combine multiple objects together to manipulate them, often in a tree structure, such as a binary tree, a number, and so on.

Combination Mode (Composite)

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.