设计模式:组合模式(Composite Pattern)
生活随笔
收集整理的這篇文章主要介紹了
设计模式:组合模式(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("兵部", "林沖部長(zhǎng)");Department department1 = new Department("禁軍系", "八十萬士兵");Department department2 = new Department("后勤系", "廚師做的一手好菜");College college2 = new College("通信部", "戴宗部長(zhǎng)");Department department3 = new Department("跑腿系", "十萬士兵");university.add(college1);college1.add(department1);college1.add(department2);university.add(college2);college2.add(department3);university.print();} }?
總結(jié)
以上是生活随笔為你收集整理的设计模式:组合模式(Composite Pattern)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式:装饰者模式(Decorator
- 下一篇: 设计模式:外观模式(Facade)