Spring Boot 实战:使用组合模式构建树形结构

一、引言

组合模式(Composite Pattern)是一种结构性设计模式,它让你可以将对象组合成树形结构以表示部分-整体层次结构。组合模式使得用户可以一致地使用单个对象和组合对象。本文将介绍如何在Spring Boot应用中实现一个基于组合模式的树形结构,并通过实际代码示例进行说明。

二、需求分析

假设我们需要为一个文件管理系统实现一个功能,该系统需要能够显示文件夹及其包含的子文件夹和文件,并且支持对这些文件和文件夹的操作。

三、设计模型

为了实现这个功能,我们首先定义两个类:FileComponent(抽象组件类)和Folder及File(具体组件类)。

  1. 抽象组件类 (FileComponent): 定义所有组件共有的操作。
  2. 具体组件类 (Folder 和 File):Folder: 可以包含其他文件或文件夹。File: 不包含任何其他组件。

四、代码实现

  1. 创建抽象组件类:

    java

深色版本

复制代码
1public abstract class FileComponent {
2    private String name;
3    private String size;
4
5    public FileComponent(String name, String size) {
6        this.name = name;
7        this.size = size;
8    }
9
10    public abstract void add(FileComponent component);
11    public abstract void remove(FileComponent component);
12    public abstract void display(int depth);
13}
  1. 创建具体组件类:

    java

深色版本

复制代码
1public class Folder extends FileComponent {
2    private List<FileComponent> children = new ArrayList<>();
3
4    public Folder(String name, String size) {
5        super(name, size);
6    }
7
8    @Override
9    public void add(FileComponent component) {
10        children.add(component);
11    }
12
13    @Override
14    public void remove(FileComponent component) {
15        children.remove(component);
16    }
17
18    @Override
19    public void display(int depth) {
20        System.out.println(String.format("%s%s [folder]", " ".repeat(depth), getName()));
21        for (FileComponent component : children) {
22            component.display(depth + 2);
23        }
24    }
25}
26
27public class File extends FileComponent {
28    public File(String name, String size) {
29        super(name, size);
30    }
31
32    @Override
33    public void add(FileComponent component) {
34        throw new UnsupportedOperationException("Cannot add to a file.");
35    }
36
37    @Override
38    public void remove(FileComponent component) {
39        throw new UnsupportedOperationException("Cannot remove from a file.");
40    }
41
42    @Override
43    public void display(int depth) {
44        System.out.println(String.format("%s%s [file]", " ".repeat(depth), getName()));
45    }
46}
  1. 创建控制器:在Spring Boot应用中添加一个REST控制器,用于展示文件系统的结构。

    java

深色版本

复制代码
1@RestController
2@RequestMapping("/file-system")
3public class FileSystemController {
4
5    @GetMapping("/display")
6    public String displayFileSystem() {
7        FileComponent root = new Folder("root", "0");
8
9        // 创建文件夹和文件
10        FileComponent folder1 = new Folder("folder1", "0");
11        FileComponent folder2 = new Folder("folder2", "0");
12        FileComponent file1 = new File("file1.txt", "10KB");
13        FileComponent file2 = new File("file2.txt", "20KB");
14
15        // 添加到根目录
16        root.add(folder1);
17        root.add(folder2);
18        root.add(file1);
19
20        // 添加到子文件夹
21        folder1.add(file2);
22
23        // 显示文件系统
24        return displayFileStructure(root);
25    }
26
27    private String displayFileStructure(FileComponent component) {
28        StringBuilder builder = new StringBuilder();
29        component.display(0);
30        return builder.toString();
31    }
32}

五、运行结果

当你运行displayFileSystem方法时,你将看到以下输出:

复制代码
深色版本

1root [folder]
2  folder1 [folder]
3    file2.txt [file]
4  folder2 [folder]
5  file1.txt [file]

六、总结

通过上述示例,我们可以看到如何使用组合模式在Spring Boot应用中构建和操作复杂的树形结构。这种方式不仅提高了代码的可读性和可维护性,同时也增强了系统的扩展能力。

相关推荐
我是一颗柠檬9 小时前
【Java后端技术亮点】动态路由权限(按钮级权限),细粒度控制到按钮级别
java·开发语言·后端·状态模式
前端Hardy9 小时前
CSS 动画真的比 JS 快?Josh Comeau 做了组实验,结果跟直觉不一样
前端·javascript·后端
Front思9 小时前
调取支付宝支付正式环境不可以唤起来,但是沙箱可以
后端
foggyprojects9 小时前
AI 生成 SQL 模板以后,为什么还需要固定 helper 规则
后端
明天一点9 小时前
Cloudflare 通知转发钉钉机器人
前端·后端
前端Hardy9 小时前
前端日历组件,要变天了?Schedule-X v4.6 彻底杀疯了
前端·javascript·后端
Oo_行者_oO9 小时前
微服务 Feign 从“万能公共服务”到“业务客户端”
后端·架构
wei_shuo9 小时前
别再踩坑了!KingbaseES 存储过程与触发器开发避坑实录
后端
元宝骑士9 小时前
MySQL 实战:跨表排序 + 指定类型置顶四种写法
后端·mysql
ConardLi10 小时前
啊?我刚开源的 Skills 已经 7K Star 了?!
前端·人工智能·后端