设计模式简述(十四)组合模式

组合模式

描述

组合模式用于描述部分与整体的关系,将个体对象与组合对象的行为统一,便于维护整个数据集。

基本使用

所有节点方法一致

  • 定义通用操作抽象组件
java 复制代码
public abstract class AbstractComponent {
    private final String name;

    protected AbstractComponent(String name) {
        this.name = name;
    }

    public void description() {
        System.out.println("name: " + this.name);
    }

    public abstract void addItem(AbstractComponent child);

    public abstract void deleteItem(AbstractComponent child);

    public abstract List<AbstractComponent> getChildren();
}
  • 定义树枝节点
java 复制代码
public class ComplexComponent extends AbstractComponent {
    private final List<AbstractComponent> children = new ArrayList<>();

    protected ComplexComponent(String name) {
        super(name);
    }

    public void addItem(AbstractComponent child) {
        children.add(child);
    }

    public void deleteItem(AbstractComponent child) {
        children.remove(child);
    }

    public List<AbstractComponent> getChildren() {
        return children;
    }
}
  • 定义叶子节点
java 复制代码
public class LeafComponent extends AbstractComponent {
    protected LeafComponent(String name) {
        super(name);
    }

    @Override
    public void addItem(AbstractComponent child) {

    }

    @Override
    public void deleteItem(AbstractComponent child) {

    }

    @Override
    public List<AbstractComponent> getChildren() {
        return null;
    }
}
使用

由于所有节点操作一致,在使用中无需强转

java 复制代码
public class Sample {
    public static void main(String[] args) {
        AbstractComponent root = new ComplexComponent("L1");
        AbstractComponent l2 = new ComplexComponent("L2");
        root.addItem(l2);
        AbstractComponent l2_1 = new ComplexComponent("L2_1");
        AbstractComponent l2_2 = new ComplexComponent("L2_2");
        l2.addItem(l2_1);
        l2.addItem(l2_2);
        AbstractComponent l2_1_1 = new ComplexComponent("L2_1_1");
        l2_1.addItem(l2_1_1);
        AbstractComponent l2_1_2 = new ComplexComponent("L2_1_2");
        l2_1.addItem(l2_1_2);
        printComponent(root);
    }

    private static void printComponent(AbstractComponent root) {
        root.description();
        root.getChildren().forEach(component -> {
            if (component instanceof ComplexComponent) {
                printComponent(component);
            } else {
                component.description();
            }
        });
    }
}

叶子无实现子节点

  • 定义通用操作的抽象组件
java 复制代码
public abstract class AbstractComponent {
    private final String name;

    protected AbstractComponent(String name) {
        this.name = name;
    }

    public void description() {
        System.out.println("name: " + this.name);
    }
}
  • 定义树枝节点
java 复制代码
public class ComplexComponent extends AbstractComponent {
    private final List<AbstractComponent> children = new ArrayList<>();

    protected ComplexComponent(String name) {
        super(name);
    }

    public void addItem(AbstractComponent child) {
        children.add(child);
    }

    public void deleteItem(AbstractComponent child) {
        children.remove(child);
    }

    public List<AbstractComponent> getChildren() {
        return children;
    }
}
  • 定义叶子节点
java 复制代码
public class LeafComponent extends AbstractComponent {
    protected LeafComponent(String name) {
        super(name);
    }
}
使用
java 复制代码
public class Sample {
    public static void main(String[] args) {
        ComplexComponent root = new ComplexComponent("L1");
        ComplexComponent l2 = new ComplexComponent("L2");
        root.addItem(l2);
        ComplexComponent l2_1 = new ComplexComponent("L2_1");
        ComplexComponent l2_2 = new ComplexComponent("L2_2");
        l2.addItem(l2_1);
        l2.addItem(l2_2);
        ComplexComponent l2_1_1 = new ComplexComponent("L2_1_1");
        l2_1.addItem(l2_1_1);
        ComplexComponent l2_1_2 = new ComplexComponent("L2_1_2");
        l2_1.addItem(l2_1_2);
        printComponent(root);
    }

    private static void printComponent(ComplexComponent root) {
        root.description();
        root.getChildren().forEach(component -> {
            if (component instanceof ComplexComponent) {
                printComponent((ComplexComponent) component);
            } else {
                component.description();
            }
        });
    }
}

添加向上查询

在有的场景中,需要支持向上查询。

可以在通用抽象中定义一个上级节点,然后在父节点添加子节点同时为子节点关联父节点

  • 定义通用抽象组件
java 复制代码
public abstract class AbstractComponent {
    private final String name;
    /**
     * 新增了一个父级成员
     */
    private AbstractComponent parent;

    protected AbstractComponent(String name) {
        this.name = name;
    }

    public void description() {
        System.out.println("name: " + this.name + "  parent: " + (Objects.isNull(getParent()) ? "null" : getParent().getName()));
    }

    public abstract void addItem(AbstractComponent child);

    public abstract void deleteItem(AbstractComponent child);
    public abstract List<AbstractComponent> getChildren();

    public void setParent(AbstractComponent parent) {
        this.parent = parent;
    }

    public AbstractComponent getParent() {
        return parent;
    }

    public String getName() {
        return name;
    }
}
  • 定义树枝节点
java 复制代码
public class ComplexComponent extends AbstractComponent {
    private final List<AbstractComponent> children = new ArrayList<>();

    protected ComplexComponent(String name) {
        super(name);
    }

    /**
     * 在添加子节点时 同时设置子节点的父级节点
     * @param child
     */
    public void addItem(AbstractComponent child) {
        children.add(child);
        child.setParent(this);
    }

    /**
     * 在移除子节点时 同时清空子节点的父级节点
     * @param child
     */
    public void deleteItem(AbstractComponent child) {
        children.remove(child);
        child.setParent(null);
    }

    public List<AbstractComponent> getChildren() {
        return children;
    }
}
  • 定义叶子节点(没变化)
java 复制代码
public class LeafComponent extends AbstractComponent {
    protected LeafComponent(String name) {
        super(name);
    }

    @Override
    public void addItem(AbstractComponent child) {

    }

    @Override
    public void deleteItem(AbstractComponent child) {

    }

    @Override
    public List<AbstractComponent> getChildren() {
        return null;
    }
}
使用(没变化)
java 复制代码
public class Sample {
    public static void main(String[] args) {
        AbstractComponent root = new ComplexComponent("L1");
        AbstractComponent l2 = new ComplexComponent("L2");
        root.addItem(l2);
        AbstractComponent l2_1 = new ComplexComponent("L2_1");
        AbstractComponent l2_2 = new ComplexComponent("L2_2");
        l2.addItem(l2_1);
        l2.addItem(l2_2);
        AbstractComponent l2_1_1 = new ComplexComponent("L2_1_1");
        l2_1.addItem(l2_1_1);
        AbstractComponent l2_1_2 = new ComplexComponent("L2_1_2");
        l2_1.addItem(l2_1_2);
        printComponent(root);
    }

    private static void printComponent(AbstractComponent root) {
        root.description();
        root.getChildren().forEach(component -> {
            if (component instanceof ComplexComponent) {
                printComponent(component);
            } else {
                component.description();
            }
        });
    }
}
相关推荐
一伦明悦დ12 小时前
嵌入式系统C语言编程常用设计模式---参数表驱动设计
c语言·开发语言·单片机·设计模式
qqxhb18 小时前
零基础设计模式——第二部分:创建型模式 - 原型模式
设计模式·原型模式·浅拷贝·深拷贝
shark-chili18 小时前
高效缓存设计的哲学
设计模式·接口·编程语言·抽象类
Your易元1 天前
设计模式-备忘录模式
java·开发语言·spring·设计模式
周努力.1 天前
设计模式之备忘录模式
设计模式·备忘录模式
pp-周子晗(努力赶上课程进度版)1 天前
【Linux】利用多路转接epoll机制、ET模式,基于Reactor设计模式实现
服务器·网络·设计模式
widder_2 天前
软考中级软件设计师——设计模式篇
单例模式·设计模式
goldfishsky2 天前
设计模式-工厂模式和策略模式
设计模式·策略模式
hope_wisdom2 天前
实战设计模式之状态模式
设计模式·系统架构·状态模式·软件工程·架构设计
mutianhao10242 天前
Python测试单例模式
python·单例模式·设计模式