Java设计模式之-组合模式

发布于:2025-07-17 ⋅ 阅读:(15) ⋅ 点赞:(0)

什么是组合模式?

组合模式允许你将对象组合成树形结构来表示"部分-整体"的层次结构。它让客户端能够以统一的方式处理单个对象对象组合

简单来说,就像公司的组织结构:

  • 公司有部门
  • 部门有小组
  • 小组有员工
  • 但无论是对公司、部门还是员工,都可以统一执行"工作"操作

主要解决什么问题?

组合模式主要解决处理树形结构数据时的问题:

  1. 客户端需要区分简单元素(叶子节点)和复杂元素(容器节点)
  2. 处理容器节点时需要递归处理其子节点
  3. 希望用统一接口处理所有节点,无论它是简单还是复杂

何时使用组合模式?

当你发现以下场景时,考虑使用组合模式:

  • 需要表示对象的部分-整体层次结构
  • 希望用户忽略组合对象与单个对象的不同
  • 结构可以形成任意深度的树形嵌套
  • 需要对整个树形结构执行统一操作(如渲染、计算等)

组合模式的优点

  1. 简化客户端代码:客户端可以一致地处理单个对象和组合对象
  2. 开闭原则:容易新增组件类型,无需修改现有代码
  3. 灵活的结构:可以构建任意复杂的树形结构
  4. 统一操作:对整个结构执行操作变得简单

组合模式的缺点

  1. 过度一般化:有时很难为所有组件定义通用接口
  2. 类型检查问题:运行时可能需要检查对象类型
  3. 设计复杂:需要仔细设计组件接口,可能变得过于抽象

代码示例:文件系统

让我们用文件系统的例子来演示组合模式:

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

// 组件抽象类(可以是接口)
abstract class FileSystemComponent {
    protected String name;
    
    public FileSystemComponent(String name) {
        this.name = name;
    }
    
    public abstract void display(int depth);
    public abstract long getSize();
    
    // 默认实现(叶子节点不需要实现)
    public void add(FileSystemComponent component) {
        throw new UnsupportedOperationException();
    }
    
    public void remove(FileSystemComponent component) {
        throw new UnsupportedOperationException();
    }
}

// 叶子节点:文件
class File extends FileSystemComponent {
    private long size;
    
    public File(String name, long size) {
        super(name);
        this.size = size;
    }
    
    @Override
    public void display(int depth) {
        System.out.println("-".repeat(depth) + name + " (" + size + " bytes)");
    }
    
    @Override
    public long getSize() {
        return size;
    }
}

// 容器节点:目录
class Directory extends FileSystemComponent {
    private List<FileSystemComponent> children = new ArrayList<>();
    
    public Directory(String name) {
        super(name);
    }
    
    @Override
    public void display(int depth) {
        System.out.println("-".repeat(depth) + "[D] " + name);
        for (FileSystemComponent component : children) {
            component.display(depth + 2);
        }
    }
    
    @Override
    public long getSize() {
        long totalSize = 0;
        for (FileSystemComponent component : children) {
            totalSize += component.getSize();
        }
        return totalSize;
    }
    
    @Override
    public void add(FileSystemComponent component) {
        children.add(component);
    }
    
    @Override
    public void remove(FileSystemComponent component) {
        children.remove(component);
    }
}

// 客户端代码
public class CompositePatternDemo {
    public static void main(String[] args) {
        // 创建文件
        File file1 = new File("document.txt", 1024);
        File file2 = new File("image.jpg", 2048);
        File file3 = new File("notes.txt", 512);
        
        // 创建子目录
        Directory subDir = new Directory("SubFolder");
        subDir.add(file2);
        subDir.add(file3);
        
        // 创建根目录
        Directory rootDir = new Directory("Root");
        rootDir.add(file1);
        rootDir.add(subDir);
        
        // 显示整个文件系统结构
        System.out.println("File System Structure:");
        rootDir.display(1);
        
        // 计算总大小
        System.out.println("\nTotal Size: " + rootDir.getSize() + " bytes");
    }
}

输出结果:

File System Structure:
- [D] Root
---document.txt (1024 bytes)
--- [D] SubFolder
-----image.jpg (2048 bytes)
-----notes.txt (512 bytes)

Total Size: 3584 bytes

实际应用场景

组合模式在Java中有许多实际应用:

  1. GUI组件:Swing/AWT中的Container和Component
  2. XML/HTML解析:DOM树结构
  3. 组织架构:公司部门人员管理
  4. 文件系统:如上面的示例
  5. 菜单系统:菜单和菜单项

总结

组合模式通过将对象组织成树形结构,让我们能够以统一的方式处理单个对象和组合对象。它特别适合表示部分-整体层次结构,使得添加新类型的组件变得容易,同时保持代码的简洁性。

关键点在于:

  • 定义一个既能代表叶子又能代表容器的抽象
  • 容器需要存储子组件并实现管理方法
  • 叶子节点实现基础行为
  • 客户端代码可以统一处理所有组件

网站公告

今日签到

点亮在社区的每一天
去签到