当前位置: 首页 > news >正文

Java 设计模式之组合模式

文章目录

  • Java 设计模式之组合模式
    • 概述
    • UML
    • 代码实现

Java 设计模式之组合模式

概述

  • 组合模式(Composite):将对象组合成树形结构以表示’部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
  • 优点:客户端可以统一处理单个对象和组合对象。支持递归组合,简化了复杂结构的处理。

UML

在这里插入图片描述

  • Component:抽象了或接口,定义叶子节点和组合节点的共同行为
  • Leaf:叶子节点,表示没有子组件。
  • Composite:组合节点,可以包含其他子组件。

代码实现

定义组件:

public abstract class Component {
    protected String name;

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

    /**
     * 显示
     *
     * @param level 第几层级
     */
    public abstract void display(int level);
}

定义叶子节点:

public class File extends Component {
    public File(String name) {
        super(name);
    }

    @Override
    public void display(int level) {
        System.out.println("-".repeat(level * 4) + "File:" + name);
    }
}

定义组合节点:

public class Directory extends Component {
    private ArrayList<Component> children = new ArrayList<>();

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

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

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

    @Override
    public void display(int level) {
        System.out.println("-".repeat(level * 4) + "Directory:" + name);
        for (Component component : children) {
            component.display(level + 1);
        }
    }
}

使用:

public class Client {
    public static void main(String[] args) {
        File file1 = new File("file1.txt");
        File file2 = new File("file2.txt");
        File file3 = new File("file3.txt");
        File file4 = new File("file4.txt");
        Directory root = new Directory("root");
        Directory dir1 = new Directory("dir1");
        Directory dir2 = new Directory("dir2");
        root.addComponent(file1);
        root.addComponent(dir1);
        dir1.addComponent(file2);
        dir1.addComponent(dir2);
        dir2.addComponent(file3);
        dir2.addComponent(file4);
        root.display(0);
    }
}

输出:

Directory:root
----File:file1.txt
----Directory:dir1
--------File:file2.txt
--------Directory:dir2
------------File:file3.txt
------------File:file4.txt

相关文章:

  • 深入浅出:在Vue中实现单点登录(SSO)的完整指南
  • 日语学习-日语知识点小记-构建基础-JLPT-N4N5阶段(6):動詞ない形について句型
  • 使用HX搭建UNI-APP云开发项目(适合新手小白与想学云开发的宝子)
  • DeepSeek进阶
  • 四、自然语言处理_08Transformer翻译任务案例
  • 基于VS2022在Windows上首次尝试开发C++ gRPC服务端和客户端的详细步骤
  • @runtime_checkable 装饰器
  • Android WindowContainer窗口结构
  • 基于springboot 以及vue前后端分离架构的求职招聘系统设计与实现
  • TCP文件传输
  • 开发一个音响控制板程序,需要从硬件架构设计、通信协议选择、核心功能实现三个层面进行系统化开发。以下是基于工业级开发流程的实施方案
  • python-leetcode-加一
  • 【vscode】VScode Remote SSH配置
  • WPF学习笔记
  • Vue 3 30天精进之旅:Day 24 - 国际化支持
  • 如何查看 Linux 服务器的 MAC 地址:深入解析与实践指南
  • JavaScript设计模式 -- 外观模式
  • 【学习资源】时间序列数据分析方法(1)
  • Dify - 创建 RAG Workflow 及 Restful HTTP 请求
  • windows第十章 数值型关联变量和控件型关联变量
  • 做网站工具 不懂代码/淘词神器
  • 安庆网站建设服务网/关键词挖掘站长
  • 做纱线的网站/惠州网站营销推广
  • 网站建设官方网站/企业推广软件
  • seo快速排名优化/手机优化大师下载2022
  • wordpress做外贸网站的劣势/培训行业seo整站优化