设计模式九-组合模式

组合模式是一种结构型设计模式,用于将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得客户端可以统一处理单个对象和组合对象,从而简化了客户端代码。

以下是一个使用Java实现组合模式的示例:

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

// 组合模式中的抽象组件类
abstract class Component {
    protected String name;

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

    public abstract void add(Component c);
    public abstract void remove(Component c);
    public abstract void display(int depth);
}

// 组合模式中的叶子节点类
class Leaf extends Component {
    public Leaf(String name) {
        super(name);
    }

    public void add(Component c) {
        // 叶子节点不支持添加操作
        System.out.println("Cannot add to a leaf");
    }

    public void remove(Component c) {
        // 叶子节点不支持删除操作
        System.out.println("Cannot remove from a leaf");
    }

    public void display(int depth) {
        // 显示叶子节点名称和深度
        System.out.println("-".repeat(depth) + name);
    }
}

// 组合模式中的组合节点类
class Composite extends Component {
    private List<Component> children = new ArrayList<Component>();

    public Composite(String name) {
        super(name);
    }

    public void add(Component c) {
        children.add(c);
    }

    public void remove(Component c) {
        children.remove(c);
    }

    public void display(int depth) {
        // 显示组合节点名称和深度,并递归显示子节点
        System.out.println("-".repeat(depth) + name);
        for (Component c : children) {
            c.display(depth + 2);
        }
    }
}

// 客户端代码
public class CompositeDemo {
    public static void main(String[] args) {
        // 创建组合节点和叶子节点
        Component root = new Composite("root");
        Component node1 = new Composite("node1");
        Component node2 = new Composite("node2");
        Component leaf1 = new Leaf("leaf1");
        Component leaf2 = new Leaf("leaf2");
        Component leaf3 = new Leaf("leaf3");

        // 将叶子节点添加到组合节点中
        root.add(node1);
        root.add(node2);
        node1.add(leaf1);
        node2.add(leaf2);
        node2.add(leaf3);

        // 显示组合节点和叶子节点的层次结构
        root.display(0);
    }
}

在上面的示例中,我们定义了一个抽象的Component类来表示组件,其中包含了添加、删除和显示组件的抽象方法。我们还定义了两个具体的子类:Leaf表示叶子节点,Composite表示组合节点。Leaf只有名称属性,而Composite有一个子组件列表。

在客户端代码中,我们创建了一个根节点root,并在其中添加了两个子节点node1node2,以及三个叶子节点leaf1leaf2leaf3。我们将叶

子节点添加到组合节点中,形成了一个树形结构。最后,我们调用根节点的display方法,递归地遍历整个树形结构并显示每个组件的名称和深度。

输出结果如下:

root
  node1
    leaf1
  node2
    leaf2
    leaf3

        可以看到,使用组合模式,我们可以方便地表示一个树形结构,并对其进行统一的操作。无论是叶子节点还是组合节点,客户端代码都可以对其进行添加、删除和显示操作,而不必关心其具体类型。这使得客户端代码更加简洁和易于维护。

        另外,组合模式还可以帮助我们在系统中添加新的组件类型,而无需修改现有的代码。例如,如果我们需要在上面的示例中添加一个新的组合节点类型node3,只需要创建一个新的Composite对象,将其添加到根节点中即可,无需修改现有的代码。

        总之,组合模式是一种非常有用的设计模式,它可以帮助我们表示“部分-整体”的层次结构,并对其进行统一的操作。在Java中,我们可以使用抽象类和接口来实现组件的基类,并使用组合来构建组件的树形结构。通过组合模式,我们可以更加灵活地表示和操作复杂的层次结构,并提高代码的可维护性和可扩展性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值