18.结构型 - 组合模式 (Composite)

1.定义

  • 用树状结构来组合对象,表示整体-部分的层次结构
  • 提供统一的方法来访问对象,忽略对象与对象集合之间的差别

2.UML类图

在这里插入图片描述

  • 角色介绍
    AbstractComponent : 抽象根节点,定义统一的访问接口
    Leaf : 具体叶子节点
    Composite : 具体枝干节点
  • 要点
    涉及到递归访问

3.UML示例代码

/**
 * Copyright (C), 2016-2020
 * FileName: AbstractComponent
 * Author: wei.zheng
 * Date: 2019/11/7 20:42
 * Description: 抽象节点: 接口设计
 */
public abstract class AbstractComponent {
    protected String name;

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

    public abstract void doSomething();

    public abstract void addChild(AbstractComponent child);

    public abstract AbstractComponent getChild(int index);

    public abstract void removeChild(AbstractComponent child);
}
/**
 * Copyright (C), 2016-2020
 * FileName: LeafComponent
 * Author: wei.zheng
 * Date: 2019/11/7 21:26
 * Description: 具体叶子节点
 */
public class LeafComponent extends AbstractComponent {
    public LeafComponent(String name) {
        super(name);
    }

    @Override
    public void doSomething() {
        System.out.println("Leaf :" + name);
    }

    @Override
    public void addChild(AbstractComponent child) {
        throw new UnsupportedOperationException("Leaf can not add child.");
    }

    @Override
    public AbstractComponent getChild(int index) {
        throw new UnsupportedOperationException("Leaf have not child.");
    }

    @Override
    public void removeChild(AbstractComponent child) {
        throw new UnsupportedOperationException("Leaf can not remove child.");
    }
}
/**
 * Copyright (C), 2016-2020
 * FileName: BranchComponent
 * Author: wei.zheng
 * Date: 2019/11/7 21:27
 * Description: 具体枝干节点
 */
public class BranchComponent extends AbstractComponent {
    private List<AbstractComponent> components = new ArrayList<>();

    public BranchComponent(String name) {
        super(name);
    }

    @Override
    public void doSomething() {
        System.out.println("Branch :" + name);
        if (null != components) {
            for (AbstractComponent component : components) {
                component.doSomething();
            }
        }
    }

    @Override
    public void addChild(AbstractComponent child) {
        components.add(child);
    }

    @Override
    public AbstractComponent getChild(int index) {
        return components.get(index);
    }

    @Override
    public void removeChild(AbstractComponent child) {
        components.remove(child);
    }

}
/**
 * Copyright (C), 2016-2020
 * FileName: Client
 * Author: wei.zheng
 * Date: 2019/11/7 21:32
 * Description: 组合模式用户类 (透明的组合模式,所有的节点结构是相同的)
 */
public class Client {

    public static void main(String[] args) {

        BranchComponent root = new BranchComponent("root");

        BranchComponent branch1 = new BranchComponent("Branch1");
        BranchComponent branch2 = new BranchComponent("Branch2");

        LeafComponent leaf1 = new LeafComponent("Leaf1");
        LeafComponent leaf2 = new LeafComponent("Leaf2");

        branch1.addChild(leaf1);
        branch2.addChild(leaf2);

        root.addChild(branch1);
        root.addChild(branch2);

        root.doSomething();
    }
}
// 运行结果
2020-01-03 19:03:59.393 5104-5104/com.example.composite I/System.out: Branch :root
2020-01-03 19:03:59.393 5104-5104/com.example.composite I/System.out: Branch :Branch1
2020-01-03 19:03:59.393 5104-5104/com.example.composite I/System.out: Leaf :Leaf1
2020-01-03 19:03:59.393 5104-5104/com.example.composite I/System.out: Branch :Branch2
2020-01-03 19:03:59.393 5104-5104/com.example.composite I/System.out: Leaf :Leaf2

4.总结

真正需要使用此设计模式的场景相对较少,作为了解。

发布了37 篇原创文章 · 获赞 0 · 访问量 563

猜你喜欢

转载自blog.csdn.net/qq_37514242/article/details/103825340
今日推荐