zoukankan      html  css  js  c++  java
  • 设计模式之Composite模式(笔记)

    组合模式:将对象组合成树形结构以表示“部分-总体”的层次结构。

    组合模式使得用户对单个对象和组合对象的使用具有一致性。
    适用场合:当需求中是体现部分与总体层次的结构时,以及希望用户能够忽略组合对象与单个对象的不同,统一地使用组合结构中的全部对象时,就应该考虑用组合模式。


    这里写图片描写叙述
    首先定义一个Componet抽象类

    public abstract class Component {
        protected String name;
    
        public Component(String name){
            this.name=name;
        }
        //抽象方法
        public abstract void add(Component c);
        public abstract void delete(Component c);
        public abstract void dispaly(int depth);
    }

    定义叶结点对象Leaf,继承Componet

    public class Leaf extends Component {
    
        public Leaf(String name) {
            super(name);
    
        }
    
        @Override
        public void add(Component c) {
            System.out.println("can not add a leaf");
    
        }
    
        @Override
        public void delete(Component c) {
            System.out.println("can not delete a leaf");
    
        }
    
        @Override
        public void dispaly(int depth) {
            char[] ch=new char[depth];
            for(int i=0;i<depth;i++){
                ch[i]='-';
            }
            System.out.println(new String(ch)+name);        
        }
    }

    结点定义枝结点Composite继承Component

    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 delete(Component c) {
            children.remove(c);
    
        }
    
        @Override
        public void dispaly(int depth) {
            char[] ch=new char[depth];
            for(int i=0;i<depth;i++){
                ch[i]='-';
            }
            System.out.println(new String(ch)+name);
            Iterator<Component> iterator=children.iterator();
            while(iterator.hasNext()){
                Component component=iterator.next();
                component.dispaly(depth+2);
            }
        }
       }

    client代码

    
           public static void main(String[] args) {
             //组合模式
            Composite root=new Composite("root");
            root.add(new Leaf("LeafA"));
            root.add(new Leaf("LeafB"));
            Composite comp=new Composite("Composite X");
            comp.add(new Leaf("LeafXA"));
            comp.add(new Leaf("LeafXB"));
            root.add(comp);
    
            Composite comp2=new Composite("Composite Y");
            comp2.add(new Leaf("LeafXYA"));
            comp2.add(new Leaf("LeafXYB"));
            root.add(comp2);
    
            root.add(new Leaf("LeafC"));
    
            root.dispaly(1);
    }

    结果:
    -root
    - - -LeafA
    - - -LeafB
    - - -Composite X
    - - - - -LeafXA
    - - - - -LeafXB
    - - -Composite Y
    - - - - -LeafXYA
    - - - - -LeafXYB
    - - -LeafC

  • 相关阅读:
    Android内存优化5 了解java GC 垃圾回收机制3
    一起刑事案件法庭辩护 z
    辩护技巧总结——律师在刑事辩护中应注意的几个问题 z
    证据对抗、证据链标准 z
    里德九步审讯法 z
    WCF服务在高并发情况下报目标积极拒绝的异常处理 z
    C#EasyHook例子C# Hook 指定进程C#注入指定进程 z
    玄机论坛Socket类库源码 当前版本 2.6.3 更新日期:10-09/2015 z
    庭审全程文字实录 z
    庭审精彩语录整理 z
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5342211.html
Copyright © 2011-2022 走看看