当前位置: 首页 > news >正文

软考 组合设计模式

组合设计模式(Composite Pattern)是结构型设计模式之一,它的核心思想是将对象组合成树形结构来表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

主要概念:

  1. 组件(Component):定义一个接口,用于访问和操作所有的对象(包括叶子节点和组合节点)。

  2. 叶子节点(Leaf):表示树的叶子节点,没有子节点,继承组件接口并实现具体行为。

  3. 组合节点(Composite):表示树中的分支节点,可以包含叶子节点或者其他组合节点,同样继承组件接口并实现具体行为,同时还会提供对子节点的管理功能(例如添加、删除)。private List<OrganizationComponent> components = new ArrayList<>();                       public void addComponent(OrganizationComponent component) {
            components.add(component);
        } 

@Override
    public void showDetails() {
        System.out.println("Department: " + name);
        for (OrganizationComponent component : components) {
            component.showDetails();
        }
    }

适用场景:

  • 当需要表示对象的“部分-整体”层次结构时。

  • 客户端希望统一对待单个对象和组合对象时。

  • 需要在系统中处理树形结构的数据时,比如图形界面中树形结构的布局。

示例:

假设我们有一个组织结构的系统,包含员工(叶子节点)和部门(组合节点),每个部门下可能包含多个员工或子部门。

// 组件接口
interface OrganizationComponent {void showDetails();
}// 叶子节点(员工)
class Employee implements OrganizationComponent {private String name;public Employee(String name) {this.name = name;}@Overridepublic void showDetails() {System.out.println("Employee: " + name);}
}// 组合节点(部门)
class Department implements OrganizationComponent {private String name;private List<OrganizationComponent> components = new ArrayList<>();public Department(String name) {this.name = name;}public void addComponent(OrganizationComponent component) {components.add(component);}@Overridepublic void showDetails() {System.out.println("Department: " + name);for (OrganizationComponent component : components) {component.showDetails();}}
}使用
public class CompositePatternDemo {public static void main(String[] args) {OrganizationComponent emp1 = new Employee("Alice");OrganizationComponent emp2 = new Employee("Bob");Department dept1 = new Department("HR");dept1.addComponent(emp1);dept1.addComponent(emp2);OrganizationComponent emp3 = new Employee("Charlie");Department dept2 = new Department("Finance");dept2.addComponent(emp3);Department headOffice = new Department("Head Office");headOffice.addComponent(dept1);headOffice.addComponent(dept2);headOffice.showDetails(); // 打印整个组织结构}
}输出
public class CompositePatternDemo {public static void main(String[] args) {OrganizationComponent emp1 = new Employee("Alice");OrganizationComponent emp2 = new Employee("Bob");Department dept1 = new Department("HR");dept1.addComponent(emp1);dept1.addComponent(emp2);OrganizationComponent emp3 = new Employee("Charlie");Department dept2 = new Department("Finance");dept2.addComponent(emp3);Department headOffice = new Department("Head Office");headOffice.addComponent(dept1);headOffice.addComponent(dept2);headOffice.showDetails(); // 打印整个组织结构}
}

http://www.xdnf.cn/news/612991.html

相关文章:

  • docker基础
  • 第36节:PyTorch基本张量操作
  • springboot配置mysql druid连接池,以及连接池参数解释
  • Python训练营打卡 Day24
  • CloudCanal RAG x Ollama 构建全栈私有 AI 服务
  • 1.2 控制系统的数学模型
  • 深入理解局域网内流量与链路监控的实战价值
  • 连续质数和
  • python web flask专题-Flask入门指南:从安装到核心功能详解
  • 比特授权云外壳加密支持Android 15!
  • DL00912-基于自监督深度聚类的高光谱目标检测含数据集
  • 大模型技术生态全景解析:从基础组件到AGI的演进之路
  • Flink初始及搭建集群环境(技术选型与实战详解)
  • 用AI工具创作出具有史诗感的神话故事短片
  • 制作一款打飞机游戏55:扩散
  • [GHCTF 2025]ret2libc1(NSSCTF)
  • Spring Bean的生命周期
  • 深度学习模型可视化:Netron的安装和使用
  • 深度学习-162-DeepSeek之调用远程大模型API接口参数结构分析
  • Socket 的两个不同含义:硬件 CPU Socket 和 网络 Socket 的区别
  • MySQL——复合查询表的内外连
  • 第十节第七部分:Arrays类、自定义排序规则Comparable、自定义比较器Comparator
  • PHP简介
  • DEEPSEEK + 其他工具的玩法
  • 深入剖析Go并发性能瓶颈:pprof实战指南
  • 力扣面试150题--路径总和
  • Stable Diffusion底模对应的VAE推荐
  • Docker端口映射与容器互联
  • 基于JSP+MySQL 服装销售系统
  • 今日学习:AOP数据脱敏|线程池|方法引用的实例|背包(0-1)及子集