深入理解设计模式之组合模式

深入理解设计模式之组合模式

1. 组合模式概述

组合模式(Composite Pattern)是一种结构型设计模式,它允许将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得客户端可以统一对待单个对象和组合对象,无需区分它们的差异。

这种模式创建了一个包含自身对象组的类,允许客户端以相同的方式处理单个对象和组合对象,简化了复杂树形结构的处理。

2. 组合模式的核心组成

  • 组件(Component): 为所有对象声明接口,管理子组件的接口
  • 叶子(Leaf): 表示组合中的叶子节点对象,没有子节点
  • 组合(Composite): 存储子组件并实现子组件相关操作

3. 组合模式基本实现

// 组件接口
interface Component {
   
    void operation();
    void add(Component component);
    void remove(Component component);
    Component getChild(int index);
}

// 叶子节点
class Leaf implements Component {
   
    private String name;
    
    public Leaf(String name) {
   
        this.name = name;
    }
    
    @Override
    public void operation() {
   
        System.out.println("Leaf " + name + " operation");
    }
    
    @Override
    public void add(Component component) {
   
        System.out.println("Cannot add to a leaf");
    }
    
    @Override
    public void remove(Component component) {
   
        System.out.println("Cannot remove from a leaf");
    }
    
    @Override
    public Component getChild(int index) {
   
        System.out.println("Cannot get child from a leaf");
        return null;
    }
}

// 组合节点
class Composite implements Component {
   
    private List<Component> children = new ArrayList<>();
    private String name;
    
    public Composite(String name) {
   
        this.name = name;
    }
    
    @Override
    public void operation() {
   
        System.out.println("Composite " + name + " operation");
        // 对所有子组件进行操作
        for (Component component : children) {
   
            component.operation();
        }
    }
    
    @Override
    public void add(Component component) {
   
        children.add(component);
    }
    
    @Override
    public void remove(Component component) {
   
        children.remove(component);
    }
    
    @Override
    public Component getChild(int index) {
   
        if (index < 0 || index >= children.size()) {
   
            return null;
        }
        return children.get(index);
    }
}

// 客户端代码
public class BasicCompositeDemo {
   
    public static void main(String[] args) {
   
        // 创建树形结构
        Composite root = new Composite("Root");
        Composite branch1 = new Composite("Branch 1");
        Composite branch2 = new Composite("Branch 2");
        Leaf leaf1 = new Leaf("Leaf 1");
        Leaf leaf2 = new Leaf("Leaf 2");
        Leaf leaf3 = new Leaf("Leaf 3");
        
        // 组装树形结构
        root.add(branch1);
        root.add(branch2);
        branch1.add(leaf1);
        branch1.add(leaf2);
        branch2.add(leaf3);
        
        // 操作整个树
        root.operation();
    }
}

4. 实际应用场景

4.1 文件系统实现

// 文件系统组件
abstract class FileSystemComponent {
   
    protected String name;
    protected String path;
    
    public FileSystemComponent(String name, String path) {
   
        this.name = name;
        this.path = path;
    }
    
    public String getName() {
   
        return name;
    }
    
    public String getPath() {
   
        return path;
    }
    
    public abstract long getSize();
    public abstract void display(int indent);
    
    // 辅助方法,生成缩进
    protected String getIndent(int indent) {
   
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indent; i++) {
   
            sb.append("  ");
        }
        return sb.toString();
    }
}

// 文件
class File extends FileSystemComponent {
   
    private long size;
    
    public File(String name, String path, long size) {
   
        super(name, path);
        this.size = size;
    }
    
    @Override
    public long getSize() {
   
        return size;
    }
    
    @Override
    public void display(int indent) {
   
        System.out.println(getIndent(indent) + "File: " + name + " (" + size + " bytes)");
    }
}

// 目录
class Directory extends FileSystemComponent {
   
    private List<FileSystemComponent> children = new ArrayList<>();
    
    public Directory
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值