• 23种设计模式之组合模式代码实例


    组合模式又叫部分整体模式,在处理类似树形结构的问题时常用。

        public class TreeNode {  
              
            private String name;  
            private TreeNode parent;  
            private Vector<TreeNode> children = new Vector<TreeNode>();  
              
            public 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;  
            }  
              
            //添加孩子节点  
            public void add(TreeNode node){  
                children.add(node);  
            }  
              
            //删除孩子节点  
            public void remove(TreeNode node){  
                children.remove(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!");  
            }  
        }  
  • 相关阅读:
    Canvas基础讲义
    封装一个DivTag
    递归深拷贝
    构造函数的执行过程
    封装一个Ajax工具函数
    数组去重
    [js开源组件开发]js多选日期控件
    自己写的表格插件autotable
    复杂表格的树形结构的添加删除行div实现
    自制html5塔防游戏
  • 原文地址:https://www.cnblogs.com/longhaolove/p/7892121.html
Copyright © 2020-2023  润新知