Java essay Java essay
首页
  • Java基础
  • Java进阶
  • 设计模式
  • 多线程
  • Java你不知道的小事
  • Spring初识
  • Spring进阶
  • SpringBoot基础
  • SpringBoot进阶
  • 什么是微服务
  • SpringCloud全家桶
  • Dubbo
  • SpringCloud Alibaba
  • Vue
  • 小程序
  • 博客搭建
  • 数据库
  • python
  • 大数据
  • 性能分析优化
  • 中间件
  • 云原生
  • 面试
  • 外卖霸王餐
  • 打工人的带饭生活
  • 30岁我该怎么办
友链
关于我
GitHub (opens new window)

Mr.Fire

全栈工程师
首页
  • Java基础
  • Java进阶
  • 设计模式
  • 多线程
  • Java你不知道的小事
  • Spring初识
  • Spring进阶
  • SpringBoot基础
  • SpringBoot进阶
  • 什么是微服务
  • SpringCloud全家桶
  • Dubbo
  • SpringCloud Alibaba
  • Vue
  • 小程序
  • 博客搭建
  • 数据库
  • python
  • 大数据
  • 性能分析优化
  • 中间件
  • 云原生
  • 面试
  • 外卖霸王餐
  • 打工人的带饭生活
  • 30岁我该怎么办
友链
关于我
GitHub (opens new window)
  • Java基础

  • Java进阶

  • 设计模式

    • 设计模式之-创建型-单例模式
    • 设计模式之-创建型-工厂模式
    • 设计模式之-创建型-抽象工厂模式
    • 设计模式之-创建型-原型模式
    • 设计模式之-创建型-建造者模式
    • 设计模式之-结构型-适配器模式
    • 设计模式之-结构型-装饰者模式
    • 设计模式之-结构型-代理模式
    • 设计模式之-结构型-外观模式
    • 设计模式之-结构型-桥接模式
    • 设计模式之-结构型-组合模式
      • 概述
      • 角色
      • 使用场景
      • 代码示例
      • 总结
    • 设计模式之-结构型-享元模式
    • 设计模式之-创建型-策略模式
    • 设计模式之-行为型-模板方法模式
    • 设计模式之-行为型-观察者模式
    • 设计模式之-行为型-迭代器模式
    • 设计模式之-行为型-命令模式
    • 设计模式之-行为型-备忘录模式
    • 设计模式之-行为型-状态模式
    • 设计模式之-行为型-访问者模式
    • 设计模式之-行为型-中介者模式
    • 设计模式之-行为型-责任链模式
    • 设计模式之-行为型-解释器模式
  • 多线程

  • 你不知道的小事

  • Java基础
  • 设计模式
Mr.Fire
2024-12-18
目录

设计模式之-结构型-组合模式

# 概述

组合模式是一种结构性设计模式,它允许将对象组合成树形结构来表示“部分-整体”的层次关系。组合模式能够使客户端以统一的方式对待个别对象和对象组合,从而简化了使用复杂对象的代码。

# 角色

组合模式主要由以下几个部分组成:

  • 组件接口(Component):定义了叶子和组合对象的共同接口。
  • 叶子类(Leaf):实现了组件接口的基本对象,不包含任何子对象。
  • 组合类(Composite):实现了组件接口,并且可以包含子组件,提供管理和操作子对象的方法。

# 使用场景

  • 需要表示对象的部分-整体层次结构时。
  • 客户端需要统一使用单个对象和对象组合的接口时。
  • 希望简化客户端代码,以便可以独立管理组件树的各个部分时。

# 代码示例

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

// 组件接口
interface Component {
    void operation();
}

// 叶子类
class Leaf implements Component {
    private String name;

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

    @Override
    public void operation() {
        System.out.println("Leaf: " + name);
    }
}

// 组合类
class Composite implements Component {
    private List<Component> children = new ArrayList<>();
    private String name;

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

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

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

    @Override
    public void operation() {
        System.out.println("Composite: " + name);
        for (Component child : children) {
            child.operation();
        }
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        Composite root = new Composite("Root");
        Leaf leaf1 = new Leaf("Leaf 1");
        Leaf leaf2 = new Leaf("Leaf 2");

        root.add(leaf1);
        root.add(leaf2);

        Composite subComposite = new Composite("SubComposite");
        Leaf leaf3 = new Leaf("Leaf 3");
        subComposite.add(leaf3);
        root.add(subComposite);

        root.operation();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

# 总结

组合模式通过将对象组合成树形结构,简化了客户端使用复杂对象的过程。它提供了一种统一的方法来处理单个对象和对象组合,使得代码更具可读性和可维护性。在需要处理层次结构的场景中,组合模式是一种非常有效的解决方案。理解和掌握组合模式对软件开发者来说是至关重要的。

最后更新时间: 2024/12/26, 17:56:54
设计模式之-结构型-桥接模式
设计模式之-结构型-享元模式

← 设计模式之-结构型-桥接模式 设计模式之-结构型-享元模式→

最近更新
01
SuperBuilder
12-29
02
30岁我该怎么办
12-29
03
关于存钱
12-29
更多文章>
Theme by Vdoing | Copyright © 2021-2025 Mr.Fire | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式