组合模式
概述
组合模式(Composite Pattern)是一种结构型设计模式,它允许你将对象组合成树形结构来表现"整体/部分"层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
定义
组合模式将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性。
结构
组合模式包含以下几个角色:
1. Component(抽象构件)
- 为组合中的对象声明接口
- 在适当的情况下,实现所有类共有接口的默认行为
- 声明一个接口用于访问和管理 Component 的子部件
- 在递归结构中定义一个接口,用于访问一个父部件,并在合适的情况下实现它
2. Leaf(叶子构件)
- 在组合中表示叶子节点对象,叶子节点没有子节点
- 在组合中定义图元对象的行为
3. Composite(容器构件)
- 定义有子部件的那些部件的行为
- 存储子部件
- 在 Component 接口中实现与子部件有关的操作
优缺点
优点
- 统一处理:客户端可以统一地处理单个对象和组合对象,无需关心处理的是单个对象还是组合对象
- 易于扩展:新增构件类型时,不需要修改现有代码,符合开闭原则
- 简化客户端代码:客户端不需要知道对象的具体类型,只需要知道抽象类型
- 树形结构:天然支持树形结构的构建和管理
缺点
- 类型检查困难:在运行时检查对象类型时,可能会遇到类型转换的问题
- 设计复杂:过度使用组合模式会使设计变得复杂
- 性能问题:对于大型树形结构,遍历和查找可能会影响性能
适用场景
- 文件系统:文件和文件夹的层次结构
- 图形界面:窗口、面板、按钮等 UI 组件的层次结构
- 组织架构:公司、部门、员工的层次结构
- 菜单系统:菜单项和子菜单的层次结构
- XML 文档:元素和子元素的层次结构
实现示例
示例 1:文件系统
// 抽象构件
public abstract class FileSystemComponent {
protected String name;
public FileSystemComponent(String name) {
this.name = name;
}
public abstract void display(int depth);
public abstract long getSize();
}
// 叶子构件 - 文件
public class File extends FileSystemComponent {
private long size;
public File(String name, long size) {
super(name);
this.size = size;
}
@Override
public void display(int depth) {
String indent = " ".repeat(depth);
System.out.println(indent + "文件: " + name + " (" + size + " bytes)");
}
@Override
public long getSize() {
return size;
}
}
// 容器构件 - 文件夹
public class Directory extends FileSystemComponent {
private List<FileSystemComponent> children = new ArrayList<>();
public Directory(String name) {
super(name);
}
public void add(FileSystemComponent component) {
children.add(component);
}
public void remove(FileSystemComponent component) {
children.remove(component);
}
@Override
public void display(int depth) {
String indent = " ".repeat(depth);
System.out.println(indent + "文件夹: " + name);
for (FileSystemComponent child : children) {
child.display(depth + 1);
}
}
@Override
public long getSize() {
long totalSize = 0;
for (FileSystemComponent child : children) {
totalSize += child.getSize();
}
return totalSize;
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
// 创建文件系统结构
Directory root = new Directory("根目录");
Directory documents = new Directory("文档");
documents.add(new File("报告.docx", 1024));
documents.add(new File("计划.xlsx", 2048));
Directory pictures = new Directory("图片");
pictures.add(new File("照片1.jpg", 5120));
pictures.add(new File("照片2.jpg", 6144));
root.add(documents);
root.add(pictures);
root.add(new File("说明.txt", 512));
// 显示文件系统结构
System.out.println("文件系统结构:");
root.display(0);
System.out.println("\n总大小: " + root.getSize() + " bytes");
}
}
示例 2:图形界面组件
// 抽象构件
public abstract class UIComponent {
protected String name;
public UIComponent(String name) {
this.name = name;
}
public abstract void render();
public abstract void add(UIComponent component);
public abstract void remove(UIComponent component);
public abstract UIComponent getChild(int index);
}
// 叶子构件 - 按钮
public class Button extends UIComponent {
public Button(String name) {
super(name);
}
@Override
public void render() {
System.out.println("渲染按钮: " + name);
}
@Override
public void add(UIComponent component) {
throw new UnsupportedOperationException("按钮不能添加子组件");
}
@Override
public void remove(UIComponent component) {
throw new UnsupportedOperationException("按钮不能移除子组件");
}
@Override
public UIComponent getChild(int index) {
throw new UnsupportedOperationException("按钮没有子组件");
}
}
// 叶子构件 - 文本框
public class TextBox extends UIComponent {
public TextBox(String name) {
super(name);
}
@Override
public void render() {
System.out.println("渲染文本框: " + name);
}
@Override
public void add(UIComponent component) {
throw new UnsupportedOperationException("文本框不能添加子组件");
}
@Override
public void remove(UIComponent component) {
throw new UnsupportedOperationException("文本框不能移除子组件");
}
@Override
public UIComponent getChild(int index) {
throw new UnsupportedOperationException("文本框没有子组件");
}
}
// 容器构件 - 面板
public class Panel extends UIComponent {
private List<UIComponent> children = new ArrayList<>();
public Panel(String name) {
super(name);
}
@Override
public void render() {
System.out.println("渲染面板: " + name);
for (UIComponent child : children) {
child.render();
}
}
@Override
public void add(UIComponent component) {
children.add(component);
}
@Override
public void remove(UIComponent component) {
children.remove(component);
}
@Override
public UIComponent getChild(int index) {
return children.get(index);
}
}
与其他模式的关系
- 与装饰器模式:组合模式关注的是对象的结构,而装饰器模式关注的是对象的行为
- 与访问者模式:组合模式可以与访问者模式结合使用,对树形结构进行遍历和操作
- 与迭代器模式:组合模式可以与迭代器模式结合使用,提供统一的遍历接口
总结
组合模式是一种非常实用的结构型设计模式,特别适用于需要表示"整体-部分"层次结构的场景。它通过统一的接口来处理单个对象和组合对象,简化了客户端代码,提高了系统的可扩展性。
在实际开发中,组合模式常用于文件系统、图形界面、组织架构等场景。合理使用组合模式可以使代码更加清晰、易于维护。
