美文网首页设计模式
组合模式(Component)

组合模式(Component)

作者: 剑道_7ffc | 来源:发表于2020-04-27 16:04 被阅读0次

一句话总结

组合对象和单个对象

内容

组合模式也叫整体和部分,以树形接口来标识整体和部分的层级结构,使单个对象和组合对象(容器)的使用具有一致性。

场景

树形菜单,操作系统的目录结构,公司的组织架构

类图

代码示例

代码实现有两种写法:透明模式和安全模式。

透明模式

所有的方法都定义在抽象根节点

// 抽象根节点
public abstract class Component {
    protected String name;

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

    public abstract String operation();

    public boolean addChild(Component component) {
        throw new UnsupportedOperationException("addChild not supported!");
    }

    public boolean removeChild(Component component) {
        throw new UnsupportedOperationException("removeChild not supported!");
    }

    public Component getChild(int index) {
        throw new UnsupportedOperationException("getChild not supported!");
    }
}
// 树节点
public class Composite extends Component {
    private List<Component> mComponents;

    public Composite(String name) {
        super(name);
        this.mComponents = new ArrayList<Component>();
    }

    @Override
    public String operation() {
        StringBuilder builder = new StringBuilder(this.name);
        for (Component component : this.mComponents) {
            builder.append("\n");
            builder.append(component.operation());
        }
        return builder.toString();
    }

    @Override
    public boolean addChild(Component component) {
        return this.mComponents.add(component);
    }

    @Override
    public boolean removeChild(Component component) {
        return this.mComponents.remove(component);
    }

    @Override
    public Component getChild(int index) {
        return this.mComponents.get(index);
    }
}
//叶子节点
public class Leaf extends Component {

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

    @Override
    public String operation() {
        return this.name;
    }
}
public class Test {
    public static void main(String[] args) {
        // 来一个根节点
        Component root = new Composite("root");
        // 来一个树枝节点
        Component branchA = new Composite("---branchA");
        Component branchB = new Composite("------branchB");
        // 来一个叶子节点
        Component leafA = new Leaf("------leafA");
        Component leafB = new Leaf("---------leafB");
        Component leafC = new Leaf("---leafC");

        root.addChild(branchA);
        root.addChild(leafC);

        branchA.addChild(leafA);
        branchA.addChild(branchB);

        branchB.addChild(leafB);

        String result = root.operation();
        System.out.println(result);

    }
}
image.png

安全模式

抽象根节点只定义共用的行为

// 抽象根节点
public abstract class Component {
    protected String name;

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

    public abstract String operation();
}
// 树节点
public class Composite extends Component {
    private List<Component> mComponents;

    public Composite(String name) {
        super(name);
        this.mComponents = new ArrayList<Component>();
    }
    @Override
    public String operation() {
        StringBuilder builder = new StringBuilder(this.name);
        for (Component component : this.mComponents) {
            builder.append("\n");
            builder.append(component.operation());
        }
        return builder.toString();
    }
    public boolean addChild(Component component) {
        return this.mComponents.add(component);
    }
    public boolean removeChild(Component component) {
        return this.mComponents.remove(component);
    }
    public Component getChild(int index) {
        return this.mComponents.get(index);
    }
}
//叶子节点
public class Leaf extends Component {

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

    @Override
    public String operation() {
        return this.name;
    }
}

源码分析

Map:抽象根节点
HashMap:树节点
Node:叶子节点

相关文章

  • 组合模式(Component)

    一句话总结 组合对象和单个对象 内容 组合模式也叫整体和部分,以树形接口来标识整体和部分的层级结构,使单个对象和组...

  • 组合模式 - Component (结构)

    *场景:类似文件夹呈现方式 打印部门所有人的工资情况场景一:表示对象整体和部分结构的时候场景二:从一个整体中能...

  • moco代码赏析九

    今天来谈一下目前的代码中是如何使用组合模式的。以下是matcher部分的类图和组合模式的类图: Component...

  • Spring 常用注解大全

    @Controller:组合注解(组合了@Component注解),应用在Controller层(控制层)。 @S...

  • Spring注解大全

    Spring使用的注解大全和解释 注解解释@Controller组合注解(组合了@Component注解),应用在...

  • 设计模式:组合模式 职责链模式

    组合模式 职责链模式 组合模式 组合模式将对象组合成树形结构,以表示“部分-整体”的层次结构。 在组合模式的树形结...

  • 第4章 结构型模式-组合模式

    一、组合模式简介 二、组合模式的优缺点 三、组合模式的使用场景 、组合模式的实例

  • 组合模式(统一叶子与组合对象)

    目录 从生活场景出发,映射组合模式 组合模式的理论概念 组合模式的实现 组合模式在源码中的应用 组合 “优于” 继...

  • 组合模式

    1. 组合模式 1.1 组合模式的定义 组合模式(Composite): 又称部分-整体模式, 将对象组合成树形结...

  • 组合模式

    设计模式系列7--组合模式 《Objective-c 编程之道 iOS 设计模式解析》 - 组合模式 常见组合模式...

网友评论

    本文标题:组合模式(Component)

    本文链接:https://www.haomeiwen.com/subject/sawywhtx.html