Java 实现组合模式


下面是将组合模式应用于目录和文件系统的实现:

接口 - 定义文件和目录的共同行为

interface FileSystemComponent {
    void display(int depth);
    void add(FileSystemComponent component);
    void remove(FileSystemComponent component);
    FileSystemComponent getChild(int index);
}

File 类 - 表示文件(叶子节点)

class File implements FileSystemComponent {
    private String name;

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

    @Override
    public void display(int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.print("  ");
        }
        System.out.println("- " + name);
    }

    @Override
    public void add(FileSystemComponent component) {
        throw new UnsupportedOperationException("Cannot add to a file");
    }

    @Override
    public void remove(FileSystemComponent component) {
        throw new UnsupportedOperationException("Cannot remove from a file");
    }

    @Override
    public FileSystemComponent getChild(int index) {
        throw new UnsupportedOperationException("File has no children");
    }
}

Directory 类 - 表示目录(复合节点)

import java.util.ArrayList;
import java.util.List;

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

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

    @Override
    public void display(int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.print("  ");
        }
        System.out.println("+ " + name);
        
        for (FileSystemComponent component : children) {
            component.display(depth + 1);
        }
    }

    @Override
    public void add(FileSystemComponent component) {
        children.add(component);
    }

    @Override
    public void remove(FileSystemComponent component) {
        children.remove(component);
    }

    @Override
    public FileSystemComponent getChild(int index) {
        return children.get(index);
    }
}

Client 类 - 使用组合模式模拟文件系统

public class FileSystemDemo {
    public static void main(String[] args) {
        // 创建文件
        FileSystemComponent file1 = new File("document.txt");
        FileSystemComponent file2 = new File("image.jpg");
        FileSystemComponent file3 = new File("notes.txt");
        FileSystemComponent file4 = new File("report.pdf");
        FileSystemComponent file5 = new File("config.ini");

        // 创建目录
        Directory root = new Directory("Root");
        Directory documents = new Directory("Documents");
        Directory images = new Directory("Images");
        Directory work = new Directory("Work");

        // 构建文件系统结构
        root.add(documents);
        root.add(images);
        root.add(file5); // 直接在根目录添加文件
        
        documents.add(file1);
        documents.add(work);
        documents.add(file3);
        
        work.add(file4);
        
        images.add(file2);

        // 显示整个文件系统
        System.out.println("File System Structure:");
        root.display(0);

        // 添加一个新文件
        System.out.println("\nAfter adding a new file:");
        documents.add(new File("presentation.pptx"));
        root.display(0);

        // 移除一个目录
        System.out.println("\nAfter removing the Images directory:");
        root.remove(images);
        root.display(0);
    }
}

代码说明

  1. FileSystemComponent 接口
    • 定义文件和目录的共同行为
    • display() 方法用于显示组件(带缩进表示层级)
    • 包含管理子组件的方法(add, remove, getChild)
  2. File 类
    • 实现 FileSystemComponent 接口
    • 代表文件(叶子节点),不能包含其他组件
    • 显示时使用 “-” 前缀
    • 对管理子组件的方法抛出异常
  3. Directory 类
    • 实现 FileSystemComponent 接口
    • 可以包含其他 FileSystemComponent(文件或目录)
    • 维护一个子组件列表
    • 显示时使用 “+” 前缀,并递归显示所有子组件
  4. FileSystemDemo 类
    • 演示如何使用组合模式模拟文件系统
    • 创建文件和目录
    • 构建层级结构
    • 显示初始结构、添加文件和移除目录后的结构

输出示例

运行该程序将输出类似以下内容:

File System Structure:
+ Root
  + Documents
    - document.txt
    + Work
      - report.pdf
    - notes.txt
  + Images
    - image.jpg
  - config.ini

After adding a new file:
+ Root
  + Documents
    - document.txt
    + Work
      - report.pdf
    - notes.txt
    - presentation.pptx
  + Images
    - image.jpg
  - config.ini

After removing the Images directory:
+ Root
  + Documents
    - document.txt
    + Work
      - report.pdf
    - notes.txt
    - presentation.pptx
  - config.ini

这个示例展示了如何使用组合模式来表示和操作文件系统的层级结构,客户端可以以统一的方式处理文件和目录。

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐