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

相关推荐
JustHappy5 小时前
古法编程秘籍(七):互联网到底是什么?把两台电脑怎么说话搞懂就够了
前端·后端·网络协议
Hommy886 小时前
【剪映小助手】添加图片接口(Add Images)
后端·github·剪映小助手·视频剪辑自动化
GetcharZp6 小时前
别再盲目用 OpenCV 读图了,这才是 CV 预处理的终极杀手锏!
后端
一 乐10 小时前
家政服务管理系统|基于springboot + vue家政服务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·家政服务管理系统
IT_陈寒10 小时前
Vite热更新失效?可能你在用Windows
前端·人工智能·后端
椰椰椰耶11 小时前
[SpringCloud][14]OpenFeign参数传递方法
后端·spring·spring cloud
onething36511 小时前
Spring Boot + Spring AI 从入门到实战:7天转型计划 Day 3 —— 消息表设计 + 级联删除 + 事务管理
人工智能·后端
荣江11 小时前
Hermes Agent 代码仓库打包工具使用指南(repomix-rs 高性能版)
后端
王某某人11 小时前
LangChain4j 入门:Java 程序员的第一个 AI 对话程序
人工智能·后端
码农刚子11 小时前
从零开始:在 Windows 服务器上部署 Node.js 项目(小白实战教程)
后端·node.js