• 组合模式


    一、定义

    组合模式(Composite):将对象组合成树形结构以表示’部分-整体‘的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

    二、优缺点

    优点:组合模式可以让客户端一致的使用组合结构和单个对象。当需求是体现部分与整体层次的结构时,以及希望可以忽略组合对象和单个对象的不同,统一使用组合中的所有对象时,就可以用组合模式。

    缺点:创建枝节点和子节点都是使用实现类,违背了依赖倒置原则。

    三、示例代码

    /*所有实现类的共有接口,用于访问和管理子部件*/
    public abstract class Component {
    
        protected String name;
    
        public Component(String name) {
            this.name = name;
        }
        public abstract void add(Component component);
        public abstract void remove(Component component);
        public abstract void display(int depth);
    
    }
    /*枝节点*/
    public class Composite extends Component {
        private List<Component> children = new ArrayList<>();
    
        public Composite(String name) {
            super(name);
        }
    
    
        @Override
        public void add(Component component) {
            children.add(component);
        }
    
        @Override
        public void remove(Component component) {
            children.remove(component);
        }
    
        @Override
        public void display(int depth) {
    
            StringBuilder stringBuilder=new StringBuilder();
            for (int i = 0; i <depth ; i++) {
                stringBuilder.append("-");
            }
            System.out.println( stringBuilder.append(name));
            for (Component component : children
            ) {
                component.display(depth + 1);
            }
        }
    }
    /*叶节点*/
    public class Leaf extends Component {
    
        public Leaf(String name) {
            super(name);
        }
    
        @Override
        public void add(Component component) {
    
        }
    
        @Override
        public void remove(Component component) {
    
        }
    
        @Override
        public void display(int depth) {
            StringBuilder stringBuilder=new StringBuilder();
            for (int i = 0; i <depth ; i++) {
                stringBuilder.append("-");
            }
            System.out.println( stringBuilder.append(name));
        }
    }
    /*客户端类*/
    public class Client {
        public static void main(String[] args) {
            Composite root = new Composite("root");
            root.add(new Leaf("Leaf A"));
            root.add(new Leaf("Leaf B"));
    
            Composite composite = new Composite("Composite X");
            composite.add(new Leaf("Leaf XA"));
            composite.add(new Leaf("Leaf XB"));
            root.add(composite);
    
            Composite composite2 = new Composite("Composite Y");
            composite2.add(new Leaf("Leaf YA"));
            composite2.add(new Leaf("Leaf YB"));
            composite.add(composite2);
            root.add(new Leaf("Leaf C"));
            root.display(1);
    
        }
    }
    
    
  • 相关阅读:
    如何成为一名专家级的开发人员
    ZapThink探讨未来十年中企业IT的若干趋势
    Adobe CTO:Android将超预期获50%份额
    我的美国之行
    用上Vista了!
    用pylint来检查python程序的潜在错误
    delegate in c++ (new version)
    The GNU Text Utilities
    python程序转为exe文件
    c++头文件,cpp文件,makefile,unit test自动生成器
  • 原文地址:https://www.cnblogs.com/yfy-/p/12220119.html
Copyright © 2020-2023  润新知