软考 组合设计模式

组合设计模式(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();

}

}

适用场景:

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

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

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

示例:

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

java 复制代码
// 组件接口
interface OrganizationComponent {
    void showDetails();
}

// 叶子节点(员工)
class Employee implements OrganizationComponent {
    private String name;
    
    public Employee(String name) {
        this.name = name;
    }

    @Override
    public 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);
    }

    @Override
    public 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(); // 打印整个组织结构
    }
}
相关推荐
workflower12 小时前
情境感知系统
人工智能·机器学习·设计模式·自然语言处理·机器人
咖啡八杯14 小时前
设计模式中的原则
设计模式·接口隔离原则·里氏替换原则·开闭原则·合成复用原则·单一职责原则
安冬的码畜日常16 小时前
【工欲善其事】深入理解 Node.js 带并发上限的异步任务批量执行逻辑
javascript·设计模式·node.js·ai编程·异步编程·并发执行
AI大法师1 天前
一个机场如何被做成 IP 场景:宝可梦机场的设计方法总结
大数据·人工智能·设计模式·新媒体运营
晚安code1 天前
干掉成山的 if-else:工厂造、策略选,一文讲透两个模式的配合
后端·设计模式
workflower2 天前
从幻觉,到现实
人工智能·深度学习·机器学习·设计模式·机器人
电子科技圈2 天前
第二代无线平台历久弥新,赋能物联网创新迭代
人工智能·嵌入式硬件·mcu·物联网·设计模式·硬件架构·iot
choumin2 天前
创建型模式——工厂方法模式
c++·设计模式·工厂方法模式·创建型模式
choumin2 天前
创建型模式——原型模式
c++·设计模式·原型模式·创建型模式
37.2℃9952 天前
Claude Design哪个公司技术好
python·设计模式