日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發(fā)布時(shí)間:2025/6/15 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式:组合模式(Composite Pattern) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? 組合模式: 又叫部分整體模式, 它創(chuàng)建了對(duì)象組的樹形結(jié)構(gòu),將對(duì)象組合成樹狀結(jié)構(gòu)以表示"整體-部分"的層次關(guān)系。

? 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){// 默認(rèn)實(shí)現(xiàn)throw new UnsupportedOperationException();}protected void remove(OrganizationComponent organizationComponent){// 默認(rèn)實(shí)現(xiàn)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("梁山大學(xué)", "成立于宋朝");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();} }

?

總結(jié)

以上是生活随笔為你收集整理的设计模式:组合模式(Composite Pattern)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。