软考 组合设计模式

组合设计模式(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(); // 打印整个组织结构
    }
}
相关推荐
码匠许师傅5 小时前
【设计模式精讲】24.观察者模式(Observer)
c++·观察者模式·设计模式·uml
小程故事多_809 小时前
从快速迭代到稳定存续,Google五大设计模式重构长效AI智能体落地逻辑
人工智能·设计模式·重构
码匠许师傅1 天前
【设计模式精讲】22.中介者模式(Mediator)
c++·设计模式·软件工程·uml·中介者模式
2401_868534781 天前
网规备考_2.4 路由协议
c++·设计模式
cpp_learner1 天前
C++ 实现责任链模式(Chain of Responsibility):从一堆 if-else 到可插拔的处理管道
c++·设计模式
码匠许师傅1 天前
【设计模式精讲】23.备忘录模式(Memento)
c++·设计模式·软件工程·uml·备忘录模式
新知图书2 天前
第8章 多智能体协同
人工智能·设计模式·智能体
京师20万禁军教头2 天前
39面向对象(高级)-设计模式
java·开发语言·设计模式
新知图书2 天前
3.5 智能体设计模式1:反应式智能体与自动导航案例
人工智能·设计模式·智能体
码匠许师傅2 天前
【设计模式精讲】21.迭代器模式(Iterator)
c++·设计模式·rpc·迭代器模式·软件工程·uml