欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > asp.net >内容正文

asp.net

设计模式:组合模式(Composite Pattern)

发布时间:2025/6/15 asp.net 51 豆豆
生活随笔 收集整理的这篇文章主要介绍了 设计模式:组合模式(Composite Pattern) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

  组合模式: 又叫部分整体模式, 它创建了对象组的树形结构,将对象组合成树状结构以表示"整体-部分"的层次关系。

  JDK中的HashMap就使用了组合模式

 

public abstract class OrganizationComponent {private String name; // 名字private String 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;}public OrganizationComponent(String name,String desc){super();this.name = name;this.desc = desc;}protected void add(OrganizationComponent organizationComponent){// 默认实现throw new UnsupportedOperationException();}protected void remove(OrganizationComponent organizationComponent){// 默认实现throw new UnsupportedOperationException();}protected abstract void print(); }import java.util.ArrayList; import java.util.List;public class University extends OrganizationComponent{List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();public University(String name, String desc) {super(name, desc);}@Overrideprotected void add(OrganizationComponent organizationComponent) {organizationComponents.add(organizationComponent);}@Overrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponents.remove(organizationComponent);}@Overridepublic String getDesc() {return super.getDesc();}@Overrideprotected void print() {System.out.println("--------"+getName()+"----------");for (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();System.out.println();}} }import java.util.ArrayList; import java.util.List;public class College extends OrganizationComponent{// 存放departmentList<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();public College(String name, String desc) {super(name, desc);}@Overrideprotected void add(OrganizationComponent organizationComponent) {organizationComponents.add(organizationComponent);}@Overrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponents.remove(organizationComponent);}@Overridepublic String getDesc() {return super.getDesc();}@Overrideprotected void print() {System.out.println(" -------"+getName()+"--------- ");for (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();}} }public class Department extends OrganizationComponent{public Department(String name, String desc) {super(name, desc);}@Overridepublic String getDesc() {return super.getDesc();}@Overrideprotected void print() {System.out.println(" ------"+getName()+"------- ");} }public class Client {public static void main(String[] args){University university = new University("梁山大学", "成立于宋朝");College college1 = new College("兵部", "林冲部长");Department department1 = new Department("禁军系", "八十万士兵");Department department2 = new Department("后勤系", "厨师做的一手好菜");College college2 = new College("通信部", "戴宗部长");Department department3 = new Department("跑腿系", "十万士兵");university.add(college1);college1.add(department1);college1.add(department2);university.add(college2);college2.add(department3);university.print();} }

 

总结

以上是生活随笔为你收集整理的设计模式:组合模式(Composite Pattern)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。