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应用中构建和操作复杂的树形结构。这种方式不仅提高了代码的可读性和可维护性,同时也增强了系统的扩展能力。

相关推荐
m0_639817151 天前
基于springboot教学资料管理系统【带源码和文档】
java·spring boot·后端
i***66501 天前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf
qq_12498707531 天前
基于springboot的建筑业数据管理系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
一 乐1 天前
宠物管理|宠物共享|基于Java+vue的宠物共享管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·springboot·宠物
IT_陈寒1 天前
Vite 5.0实战:10个你可能不知道的性能优化技巧与插件生态深度解析
前端·人工智能·后端
z***3351 天前
SQL Server2022版+SSMS安装教程(保姆级)
后端·python·flask
p***93031 天前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
zxguan1 天前
Springboot 学习 之 下载接口 HttpMessageNotWritableException
spring boot·后端·学习
加洛斯1 天前
告别数据混乱!精通Spring Boot序列化与反序列化
后端