设计模式的定义

1 组合模式:

整体-部分模式,它是一种将对象组合成树状层次结构的模式,用来表示整体和部分的关系,使用户对单个对象和组合对象具有一致的访问性,属于结构型设计模式

1.1 特点:

  1. 组合模式使得客户端代码可以一致的处理单个对象和组合对象
  2. 更容易在组合体内加入新的对象,客户端不会因为加入新的对象而改变原代码,满足''开闭原则''

1.2 缺点:

  1. 设计较复杂,客户端需要花更多的时间理清类之间的关系
  2. 不容易限制容器中的构建
  3. 不容易用继承的方法来增加构件的新功能 

1.3 结构:

  1. 抽象构件角色:
  2. 树叶构件角色:
  3. 树枝构件角色/中间构件: 

1.4 组合模式分为透明式的组合模式和安全式的组合模式。 

  1. 透明式:抽象构件声明了所有子类中的全部方法,客户端无需区别树叶对象和树枝对象
  2. 安全式:将管理子构件的方法移到树枝构件中,抽象构件和树叶构件没有对子对象的管理方法

1.5 代码实现

透明模式

public class CompositePattern {
    public static void main(String[] args) {
        Component c0 = new Composite();
        Component c1 = new Composite();
        Component leaf1 = new Leaf("1");
        Component leaf2 = new Leaf("2");
        Component leaf3 = new Leaf("3");
        c0.add(leaf1);
        c0.add(c1);
        c1.add(leaf2);
        c1.add(leaf3);
        c0.operation();
    }
}
//抽象构件
interface Component {
    public void add(Component c);
    public void remove(Component c);
    public Component getChild(int i);
    public void operation();
}
//树叶构件
class Leaf implements Component {
    private String name;
    public Leaf(String name) {
        this.name = name;
    }
    public void add(Component c) {
    }
    public void remove(Component c) {
    }
    public Component getChild(int i) {
        return null;
    }
    public void operation() {
        System.out.println("树叶" + name + ":被访问!");
    }
}
//树枝构件
class Composite implements Component {
    private ArrayList<Component> children = new ArrayList<Component>();
    public void add(Component c) {
        children.add(c);
    }
    public void remove(Component c) {
        children.remove(c);
    }
    public Component getChild(int i) {
        return children.get(i);
    }
    public void operation() {
        for (Object obj : children) {
            ((Component) obj).operation();
        }
    }
}
树叶1:被访问!
树叶2:被访问!
树叶3:被访问!

安全模式

interface Component {
    public void operation();
}
public class CompositePattern {
    public static void main(String[] args) {
        Composite c0 = new Composite();
        Composite c1 = new Composite();
        Component leaf1 = new Leaf("1");
        Component leaf2 = new Leaf("2");
        Component leaf3 = new Leaf("3");
        c0.add(leaf1);
        c0.add(c1);
        c1.add(leaf2);
        c1.add(leaf3);
        c0.operation();
    }
}

相关推荐

  1. 设计模式定义

    2023-12-09 11:40:03       52 阅读
  2. 设计模式-GOF对各个模式定义

    2023-12-09 11:40:03       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 11:40:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 11:40:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 11:40:03       82 阅读
  4. Python语言-面向对象

    2023-12-09 11:40:03       91 阅读

热门阅读

  1. springboot下rest接口抛异常的定制处理

    2023-12-09 11:40:03       62 阅读
  2. redis.conf详解之replica-read-only

    2023-12-09 11:40:03       51 阅读
  3. clickhouse删除partition分区数据

    2023-12-09 11:40:03       61 阅读
  4. Thinkphp5+FastAdmin配置workerman消息推送(多线程)

    2023-12-09 11:40:03       56 阅读
  5. 【Linux 软连接命令】

    2023-12-09 11:40:03       48 阅读
  6. GO设计模式——15、责任链模式(行为型)

    2023-12-09 11:40:03       62 阅读
  7. Android 获取进程名称

    2023-12-09 11:40:03       63 阅读
  8. 前端学习--React(5)

    2023-12-09 11:40:03       51 阅读
  9. React查询、搜索类功能的实现

    2023-12-09 11:40:03       59 阅读
  10. ReactJs笔记摘录

    2023-12-09 11:40:03       71 阅读
  11. K8S学习指南(2)-docker的基本使用

    2023-12-09 11:40:03       50 阅读
  12. Solidity学习教程

    2023-12-09 11:40:03       49 阅读