设计模式:组合模式示例

组合模式的典型例子通常涉及到树形结构的处理,下面是几个形象且易于理解的例子:

文件系统

在文件系统中,目录可以包含文件或者其他目录,但是从用户的角度来看,目录和文件都可以被"打开"或者"获取大小"。这里的目录就是一个组合对象,文件是叶子节点。

java 复制代码
interface FileSystemItem {
    void showProperties();
    long getSize();
}

class File implements FileSystemItem {
    private String name;
    private long size;

    public File(String name, long size) {
        this.name = name;
        this.size = size;
    }

    @Override
    public void showProperties() {
        System.out.println("File: " + name + " (Size: " + size + ")");
    }

    @Override
    public long getSize() {
        return size;
    }
}

class Directory implements FileSystemItem {
    private String name;
    private List<FileSystemItem> children = new ArrayList<>();

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

    public void add(FileSystemItem item) {
        children.add(item);
    }

    @Override
    public void showProperties() {
        System.out.println("Directory: " + name);
        for (FileSystemItem child : children) {
            child.showProperties();
        }
    }

    @Override
    public long getSize() {
        long size = 0;
        for (FileSystemItem child : children) {
            size += child.getSize();
        }
        return size;
    }
}

图形界面组件

在图形用户界面(GUI)中,容器组件可以包含其他容器组件或者叶子组件(如按钮、文本框等)。无论是容器还是叶子组件,都可以对它们执行某些操作,如绘制、启用/禁用等。

java 复制代码
interface GUIComponent {
    void render();
}

class Button implements GUIComponent {
    @Override
    public void render() {
        System.out.println("Render Button");
    }
}

class Panel implements GUIComponent {
    private List<GUIComponent> children = new ArrayList<>();

    public void add(GUIComponent component) {
        children.add(component);
    }

    @Override
    public void render() {
        System.out.println("Render Panel");
        for (GUIComponent child : children) {
            child.render();
        }
    }
}

组织结构

在组织结构中,公司可以分为部门,部门下可以有子部门或员工。部门和员工都可以执行某些操作,如获取成本。

java 复制代码
interface OrganizationComponent {
    void printStructure();
    double getCost();
}

class Employee implements OrganizationComponent {
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public void printStructure() {
        System.out.println("Employee: " + name + ", Salary: " + salary);
    }

    @Override
    public double getCost() {
        return salary;
    }
}

class Department implements OrganizationComponent {
    private String name;
    private List<OrganizationComponent> members = new ArrayList<>();

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

    public void add(OrganizationComponent component) {
        members.add(component);
    }

    @Override
    public void printStructure() {
        System.out.println("Department: " + name);
        for (OrganizationComponent member : members) {
            member.printStructure();
        }
    }

    @Override
    public double getCost() {
        double cost = 0;
        for (OrganizationComponent member : members) {
            cost += member.getCost();
        }
        return cost;
    }
}

在这些例子中,组合模式允许客户端以统一的方式操作单个对象和组合对象,这样的设计简化了客户端代码,并使得整个结构更加灵活。

相关推荐
这是谁的博客?8 小时前
微服务架构设计模式深度解析:从拆分策略到容灾机制
微服务·设计模式·云原生·架构·架构设计·后端开发·分布式系统
fan_music12 小时前
设计模式学习
c++·设计模式
乐观的山里娃14 小时前
【后编码时代 06】Vibe Coding + Superpowers 完全不够
设计模式·软件工程·ai编程
likerhood14 小时前
设计模式 · 责任链模式(Chain of Responsibility Pattern)
设计模式·责任链模式
追烽少年x17 小时前
STL中的设计模式(一)
c++·设计模式
乐观的山里娃17 小时前
【设计模式 10】抽象工厂:整体换季
设计模式
贵慜_Derek19 小时前
《从零实现 Agent 系统》连载 08|编排与工作流:从 Chat 到任务图
人工智能·设计模式·架构
W.W.H.20 小时前
C++ 设计模式:6 个常用模式的实战示例
开发语言·c++·设计模式
老码观察20 小时前
设计模式实战解读(三):模板方法模式——骨架复用与扩展点设计
设计模式·模板方法模式
雪度娃娃20 小时前
行为型设计模式——状态模式
ui·设计模式·状态模式