龙岗做网站的,坂田做网站多少钱,微信公众平台登录入口内村完小,查企业app文章目录 组合模式简介结构UML图具体实现UML图代码实现 组合模式简介
组合模式#xff08;Composite Pattern#xff09;又叫整体模式#xff0c;它创建了对象组的树形结构#xff0c;将对象组合成树状结构来表示“整体-部分”的层次关系。实际使用点#xff1a;HashMap中… 文章目录 组合模式简介结构UML图具体实现UML图代码实现 组合模式简介
组合模式Composite Pattern又叫整体模式它创建了对象组的树形结构将对象组合成树状结构来表示“整体-部分”的层次关系。实际使用点HashMap中构造函数有可以添加整个集合的构造就是组合模式。
结构
1.Component抽象根节点定义系统各层次对象的共有方法和属性用于访问和管理Component子部件Component可以是抽象类或者接口。2.Composite:非叶子节点用来存储子部件实现了Component结构并用 组合 关系 组合Component对象一般用来增加add、删除delete3.Leaf叶子节点叶子节点没有子节点。
UML图 具体实现
例子一个学校University有多个学院College一个学院有多个专业Department输出这个学校的树形结构。
UML图 代码实现
抽象根节点
package com.xxliao.pattern.structure.composite.demo;/*** author xxliao* description: 组合模式的 抽象根节点* date 2024/5/25 13:42*/
public abstract class OrganizationComponent {private String name;private String desc;public OrganizationComponent(String name, String desc) {this.name name;this.desc desc;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc desc;}/*** description add* author xxliao* date 2024/5/25 13:45*/public void add(OrganizationComponent organizationComponent) {// 抽象根节点不能添加throw new UnsupportedOperationException();}/*** description delete* author xxliao* date 2024/5/25 13:45*/public void delete(OrganizationComponent organizationComponent) {// 抽象根节点不能添加throw new UnsupportedOperationException();}/*** description print* author xxliao* date 2024/5/25 13:46*/public abstract void print();
}非叶子节点
package com.xxliao.pattern.structure.composite.demo;import java.util.ArrayList;
import java.util.List;/*** author xxliao* description: Composite 类 大学类 可以管理 College类* date 2024/5/25 13:48*/
public class University extends OrganizationComponent{// 子节点集合 采用组合关系 集成了 OrganizationComponent抽象根节点private ListOrganizationComponent organizationComponents new ArrayListOrganizationComponent();public University(String name, String desc) {super(name, desc);}Overridepublic String getName() {return super.getName();}Overridepublic String getDesc() {return super.getDesc();}Overridepublic void add(OrganizationComponent organizationComponent) {organizationComponents.add(organizationComponent);}Overridepublic void delete(OrganizationComponent organizationComponent) {organizationComponents.remove(organizationComponent);}Overridepublic void print() {System.out.println(getName());for (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();}}
}package com.xxliao.pattern.structure.composite.demo;import java.util.ArrayList;
import java.util.List;/*** author xxliao* description: Composite 类 学院类 可以管理 Department类* date 2024/5/25 13:48*/
public class College extends OrganizationComponent{// 子节点集合 采用组合关系 集成了 OrganizationComponent抽象根节点private ListOrganizationComponent organizationComponents new ArrayListOrganizationComponent();public College(String name, String desc) {super(name, desc);}Overridepublic String getName() {return super.getName();}Overridepublic String getDesc() {return super.getDesc();}Overridepublic void add(OrganizationComponent organizationComponent) {organizationComponents.add(organizationComponent);}Overridepublic void delete(OrganizationComponent organizationComponent) {organizationComponents.remove(organizationComponent);}Overridepublic void print() {System.out.println(getName());for (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();}}
}叶子节点
package com.xxliao.pattern.structure.composite.demo;/*** author xxliao* description: leaf 类 子节点类* date 2024/5/25 13:48*/
public class Department extends OrganizationComponent{public Department(String name, String desc) {super(name, desc);}Overridepublic String getName() {return super.getName();}Overridepublic String getDesc() {return super.getDesc();}Overridepublic void print() {System.out.println(getName());}}测试客户端
package com.xxliao.pattern.structure.composite.demo;/*** author xxliao* description: 组合模式 测试客户端* date 2024/5/25 13:57*/
public class Client {public static void main(String[] args) {// 创建大学OrganizationComponent university new University(xxliao大学, TOP1);// 创建学院OrganizationComponent computer new College(计算机学院, 计算机学院);OrganizationComponent finance new College(经济管理学院, 经管学院);// 将学院添加到大学中university.add(computer);university.add(finance);//创建专业computer.add(new Department(计算机科学与技术,计算机科学与技术));computer.add(new Department(软件工程,软件工程));computer.add(new Department(网络工程,网络工程));finance.add(new Department(经济学,经济学));finance.add(new Department(国际金融与贸易,国际金融与贸易));// 打印整个大学结构university.print();}
}测试结果