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

设计模式:组合模式示例

组合模式的典型例子通常涉及到树形结构的处理,下面是几个形象且易于理解的例子:

文件系统

在文件系统中,目录可以包含文件或者其他目录,但是从用户的角度来看,目录和文件都可以被“打开”或者“获取大小”。这里的目录就是一个组合对象,文件是叶子节点。

interface FileSystemItem {void showProperties();long getSize();
}class File implements FileSystemItem {private String name;private long size;public File(String name, long size) {this.name = name;this.size = size;}@Overridepublic void showProperties() {System.out.println("File: " + name + " (Size: " + size + ")");}@Overridepublic long getSize() {return size;}
}class Directory implements FileSystemItem {private String name;private List<FileSystemItem> children = new ArrayList<>();public Directory(String name) {this.name = name;}public void add(FileSystemItem item) {children.add(item);}@Overridepublic void showProperties() {System.out.println("Directory: " + name);for (FileSystemItem child : children) {child.showProperties();}}@Overridepublic long getSize() {long size = 0;for (FileSystemItem child : children) {size += child.getSize();}return size;}
}

图形界面组件

在图形用户界面(GUI)中,容器组件可以包含其他容器组件或者叶子组件(如按钮、文本框等)。无论是容器还是叶子组件,都可以对它们执行某些操作,如绘制、启用/禁用等。

interface GUIComponent {void render();
}class Button implements GUIComponent {@Overridepublic void render() {System.out.println("Render Button");}
}class Panel implements GUIComponent {private List<GUIComponent> children = new ArrayList<>();public void add(GUIComponent component) {children.add(component);}@Overridepublic void render() {System.out.println("Render Panel");for (GUIComponent child : children) {child.render();}}
}

组织结构

在组织结构中,公司可以分为部门,部门下可以有子部门或员工。部门和员工都可以执行某些操作,如获取成本。

interface OrganizationComponent {void printStructure();double getCost();
}class Employee implements OrganizationComponent {private String name;private double salary;public Employee(String name, double salary) {this.name = name;this.salary = salary;}@Overridepublic void printStructure() {System.out.println("Employee: " + name + ", Salary: " + salary);}@Overridepublic double getCost() {return salary;}
}class Department implements OrganizationComponent {private String name;private List<OrganizationComponent> members = new ArrayList<>();public Department(String name) {this.name = name;}public void add(OrganizationComponent component) {members.add(component);}@Overridepublic void printStructure() {System.out.println("Department: " + name);for (OrganizationComponent member : members) {member.printStructure();}}@Overridepublic double getCost() {double cost = 0;for (OrganizationComponent member : members) {cost += member.getCost();}return cost;}
}

在这些例子中,组合模式允许客户端以统一的方式操作单个对象和组合对象,这样的设计简化了客户端代码,并使得整个结构更加灵活。

http://www.lryc.cn/news/332974.html

相关文章:

  • 普通情况和高并发时,Redis缓存和数据库怎么保持一致?
  • Django -- 自动化测试
  • NodeJS 在Windows / Mac 上实现多版本控制
  • Web3 游戏周报(3.24-3.30)
  • 算法思想1. 分治法2. 动态规划法3. 贪心算法4. 回溯法
  • SpringBoot+ECharts+Html 地图案例详解
  • 达梦数据库 优化
  • 数据如何才能供得出、流得动、用得好、还安全
  • idea开发 java web 酒店推荐系统bootstrap框架开发协同过滤算法web结构java编程计算机网页
  • Linux——线程控制
  • 【Leetcode 347】,前k个高频元素,小根堆的调整
  • 【图论】【分类讨论】LeetCode3017按距离统计房屋对数目
  • 浅谈Yum 安装和 源码安装
  • JavaEE初阶Day 3:多线程(1)
  • gutil140.dll是什么?gutil140.dll无法继续执行的解决方法
  • 在CentOS 7上安装Python 3.7.7
  • 基于SpringBoot Vue宠物领养系统
  • ip命令
  • 【Kaggle】练习赛《鲍鱼年龄预测》(上)
  • Ruby 之交租阶段信息生成
  • RUST语言值所有权之内存复制与移动
  • 【Django学习笔记(三)】BootStrap介绍
  • ClickHouse开发相关(UDAF)
  • MySql并发事务问题
  • Windows下Docker创建Mysql5.7
  • Redis(性能管理、主从复制、哨兵模式)概述及部署
  • LabVIEW挖坑指南
  • docker容器环境安装记录(MAC M1)(完善中)
  • Linux 常用命令(持续更新中...)
  • xss.pwnfunction-Jefff