设计模式-23种设计模式-结构型-组合模式
一、组合模式介绍
二、组合模式引入
需求:
UML类图:
代码实现(Java):
public abstract class OrganizationComponent { private String name; // 名字 private String des; // 说明 protected void add(OrganizationComponent organizationComponent) { //默认实现 throw new UnsupportedOperationException(); } protected void remove(OrganizationComponent organizationComponent) { //默认实现 throw new UnsupportedOperationException(); } //构造器 public OrganizationComponent(String name, String des) { super(); this.name = name; this.des = des; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDes() { return des; } public void setDes(String des) { this.des = des; } //方法print, 做成抽象的, 子类都需要实现 protected abstract void print(); }
import java.util.ArrayList; import java.util.List; //University 就是 Composite , 可以管理College public class University extends OrganizationComponent { List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>(); // 构造器 public University(String name, String des) { super(name, des); } // 重写add @Override protected void add(OrganizationComponent organizationComponent) { organizationComponents.add(organizationComponent); } // 重写remove @Override protected void remove(OrganizationComponent organizationComponent) { organizationComponents.remove(organizationComponent); } @Override public String getName() { return super.getName(); } @Override public String getDes() { return super.getDes(); } // print方法,就是输出University 包含的学院 @Override protected void print() { System.out.println("--------------" + getName() + "--------------"); //遍历 organizationComponents for (OrganizationComponent organizationComponent : organizationComponents) { organizationComponent.print(); } } }
import java.util.ArrayList; import java.util.List; public class College extends OrganizationComponent { //List 中 存放的Department List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>(); // 构造器 public College(String name, String des) { super(name, des); } // 重写add @Override protected void add(OrganizationComponent organizationComponent) { //将来实际业务中,Colleage 的 add 和 University add 不一定完全一样 organizationComponents.add(organizationComponent); } // 重写remove @Override protected void remove(OrganizationComponent organizationComponent) { organizationComponents.remove(organizationComponent); } @Override public String getName() { return super.getName(); } @Override public String getDes() { return super.getDes(); } // print方法,就是输出University 包含的学院 @Override protected void print() { System.out.println("--------------" + getName() + "--------------"); //遍历 organizationComponents for (OrganizationComponent organizationComponent : organizationComponents) { organizationComponent.print(); } } }
public class Department extends OrganizationComponent { //没有集合 public Department(String name, String des) { super(name, des); } //add , remove 就不用写了,因为他是叶子节点 @Override public String getName() { return super.getName(); } @Override public String getDes() { return super.getDes(); } @Override protected void print() { System.out.println(getName()); } }
public class Client { public static void main(String[] args) { //从大到小创建对象 学校 OrganizationComponent university = new University("清华大学", " 中国顶级大学 "); //创建 学院 OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 "); OrganizationComponent infoEngineercollege = new College("信息工程学院", " 信息工程学院 "); //创建各个学院下面的系(专业) computerCollege.add(new Department("软件工程", " 软件工程不错 ")); computerCollege.add(new Department("网络工程", " 网络工程不错 ")); computerCollege.add(new Department("计算机科学与技术", " 计算机科学与技术是老牌的专业 ")); // infoEngineercollege.add(new Department("通信工程", " 通信工程不好学 ")); infoEngineercollege.add(new Department("信息工程", " 信息工程好学 ")); //将学院加入到 学校 university.add(computerCollege); university.add(infoEngineercollege); //university.print(); infoEngineercollege.print(); } }
三、组合模式在JDK集合的源码分析
import java.util.HashMap; import java.util.Map; public class Composite { public static void main(String[] args) { //说明 //1. Map 就是一个抽象的构建 (类似我们的Component) //2. HashMap是一个中间的构建(Composite), 实现/继承了相关方法 // put, putall //3. Node 是 HashMap的静态内部类,类似Leaf叶子节点, 这里就没有put, putall // static class Node<K,V> implements Map.Entry<K,V> Map<Integer,String> hashMap=new HashMap<Integer,String>(); hashMap.put(0, "东游记");//直接存放叶子节点(Node) Map<Integer,String> map=new HashMap<Integer,String>(); map.put(1, "西游记"); map.put(2, "红楼梦"); //.. hashMap.putAll(map); System.out.println(hashMap); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器