码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式——组合模式

时间:2020-03-25 19:12:06      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:节点   child   getname   bsp   vat   层次结构   als   对象   treenode   

//树节点

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
     private int id;
     private String name;
     private List<TreeNode> child = new ArrayList<TreeNode>();
     public TreeNode(int id, String name) {
         super();
         this.id = id;
         this.name = name;
     }
     public TreeNode(int id, String name, List<TreeNode> child) {
         super();
         this.id = id;
         this.name = name;
         this.child = child;
     }
     public int getId() {
         return id;
     }
     public void setId(int id) {
         this.id = id;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public List<TreeNode> getChild() {
         return child;
     }
     public void setChild(List<TreeNode> child) {
         this.child = child;
     }
     public void add(TreeNode treeNode) {
         this.child.add(treeNode);
     }
     public boolean remove(TreeNode treeNode) {
         if (child.contains(treeNode)) {
             this.child.remove(treeNode);
             return true;
         }
         return false;
     }
}

//树

public class Tree {
     //将对象组合成树形结构来表现”部分-整体“的层次结构
     private String name;
     private TreeNode root;
     //参数顺序也支持
     public Tree(TreeNode root, String name) {
         super();
         this.root = root;
         this.name = name;
     }
     public Tree(String name, TreeNode root) {
         super();
         this.name = name;
         this.root = root;
     }   
}

设计模式——组合模式

标签:节点   child   getname   bsp   vat   层次结构   als   对象   treenode   

原文地址:https://www.cnblogs.com/macro-renzhansheng/p/12568533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!