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

相关推荐
Justin3go3 小时前
HUNT0 上线了——尽早发布,尽早发现
前端·后端·程序员
Tony Bai4 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
一线大码5 小时前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
weixin_425023005 小时前
Spring Boot 配置文件优先级详解
spring boot·后端·python
weixin_425023005 小时前
Spring Boot 实用核心技巧汇总:日期格式化、线程管控、MCP服务、AOP进阶等
java·spring boot·后端
一线大码5 小时前
Java 8-25 各个版本新特性总结
java·后端
VX:Fegn08956 小时前
计算机毕业设计|基于springboot + vue校园社团管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计
To Be Clean Coder6 小时前
【Spring源码】通过 Bean 工厂获取 Bean 的过程
java·后端·spring
weixin199701080166 小时前
闲鱼 item_get - 商品详情接口对接全攻略:从入门到精通
java·后端·spring
自己的九又四分之三站台8 小时前
导入数据到OG GraphQL以及创建graph
java·后端·graphql