zoukankan      html  css  js  c++  java
  • 设计模式之组合模式 Composite

     代码实现

    //Component(抽象构件):抽象构件中定义了叶子和容器构件的共同点。比如,有公共的添加删除叶子功能,有显示节点功能。
    public abstract class Component {
        protected String name;
        public Component(String name) {
            super();
            this.name = name;
        }
        public abstract void add(Component c);
        public abstract void remove(Component c);
        public abstract void display(int depth);
    }
    抽象组件
    //表示该节点下面没有其他子节点了,就称为叶子
    public class Leaf extends Component {
        public Leaf(String name) {
            super(name);
        }
        @Override
        public void add(Component c) {
            System.out.println("leaf no add");
        }
        @Override
        public void remove(Component c) {
            System.out.println("leaf no remove");
        }
        @Override
        public void display(int depth) {
            StringBuffer sb = new StringBuffer("-");
            for (int i = 0; i <= depth; i++) {
                sb.append("-");
            }
            System.out.println(sb.toString()+name);
        }
    }
    叶子结点
    //容器构件,该节点下还有其他子节点,理解为一个容器,里面包含了其他子节点。就叫做容器构件
    public class Composite extends Component{
        private List<Component> children = new ArrayList<Component>();
        public Composite(String name) {
            super(name);
        }
        @Override
        public void add(Component c) {
            children.add(c);
        }
        @Override
        public void remove(Component c) {
            children.remove(c);
        }
        @Override
        public void display(int depth) {
            StringBuffer sb = new StringBuffer("-");
            for (int i = 0; i <= depth; i++) {
                sb.append("-");
            }
            System.out.println(sb.toString()+name);
            for (Component com : children) {
                com.display(depth + 2);
            }
        }
    }
    容器构件
    public static void main(String[] args) {
        Composite root = new Composite("root");
        root.add(new Leaf("Leaf A"));
        root.add(new Leaf("Leaf B"));
         
        Composite comp = new Composite("Composite X");
        comp.add(new Leaf("Leaf XA"));
        comp.add(new Leaf("Leaf XB"));
        root.add(comp);
         
        Composite comp2 = new Composite("Composite XY");
        comp2.add(new Leaf("Leaf XYA"));
        comp2.add(new Leaf("Leaf XYB"));
        comp.add(comp2);
         
        root.add(new Leaf("Leaf C"));
        Leaf leaf = new Leaf("Leaf D");
        root.add(leaf);
    //  root.remove(leaf);//这里可以删除某节点
        root.display(1);
    }
    构建树形结构调用
    ---root
    
    -----Leaf A
    
    -----Leaf B
    
    -----Composite X
    
    -------Leaf XA
    
    -------Leaf XB
    
    -------Composite XY
    
    ---------Leaf XYA
    
    ---------Leaf XYB
    
    -----Leaf C
    
    -----Leaf D
    打印结果
  • 相关阅读:
    WebView自适应屏幕
    shell脚本:遍历删除
    查看Mysql执行计划
    Spring 源码学习(八) AOP 使用和实现原理
    Java:控制反转(IoC)与依赖注入(DI)
    浏览器-开发者工具
    查看kafka消息消费情况
    shell脚本:遍历删除文本内路径上文件
    聚簇索引与非聚簇索引(也叫二级索引)
    有关MySQL
  • 原文地址:https://www.cnblogs.com/qingdaofu/p/7473036.html
Copyright © 2011-2022 走看看