Java結(jié)構型設計模式之組合模式Composite Pattern詳解
概述
組合模式(Composite Pattern),又叫部分整體模式,它創(chuàng)建了對象組的樹形結(jié)構,將對象組合成樹狀結(jié)構以表示“整體-部分”的層次關系。組合模式依據(jù)樹形結(jié)構來組合對象,用來表示部分以及整體層次。
這種類型的設計模式屬于結(jié)構型模式。
組合模式使得用戶對單個對象和組合對象的訪問具有一致性,即:組合能讓客戶以一致的方式處理個別對象以及組合對象。
三大組件
Component :這是組合中對象聲明接口,在適當情況下,實現(xiàn)所有類共有的接口默認行為,用于訪問和管理Component 子部件, Component 可以是抽象類或者接口
Leaf : 在組合中表示葉子節(jié)點,葉子節(jié)點沒有子節(jié)點,其定義組合內(nèi)元素的行為。
Composite :非葉子節(jié)點, 用于存儲子部件, 在Component 接口中實現(xiàn)子部件的相關操作,比如增加(add),刪除,但是可能沒有子結(jié)點(組件元素)行為。
應用案例
編寫程序展示一個學校院系結(jié)構:需求是這樣,要在一個頁面中展示出學校的院系組成,一個學校有多個學院,一個學院有多個系。
傳統(tǒng)方案可能按照 學校-學院-系別處理。將學院看做是學校的子類,系是學院的子類,這樣實際上是站在組織大小來進行分層次的。實際上我們的要求是:在一個頁面中展示出學校的院系組成,一個學校有多個學院,一個學院有多個系, 因此這種方案,不能很好實現(xiàn)的管理的操作,比如對學院、系的添加,刪除,遍歷等
解決方案:把學校、院、系都看做是組織結(jié)構,他們之間沒有繼承的關系,而是一個樹形結(jié)構,可以更好的實現(xiàn)管理操作 => 組合模式。
頂層組件OrganizationComponent
public abstract class OrganizationComponent { private String name; // 名字 private String des; // 說明 protected void add(OrganizationComponent organizationComponent) { //默認實現(xiàn) throw new UnsupportedOperationException(); } protected void remove(OrganizationComponent organizationComponent) { //默認實現(xiàn) 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, 做成抽象的, 子類都需要實現(xiàn) protected abstract void print(); }
Composite組件
//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(); } } } // 可以管理Department 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) { // 將來實際業(yè)務中,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(); } } }
葉子節(jié)點
public class Department extends OrganizationComponent { //沒有集合 public Department(String name, String des) { super(name, des); } //add , remove 就不用寫了,因為他是葉子節(jié)點 @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) { //從大到小創(chuàng)建對象 學校 OrganizationComponent university = new University("清華大學", " 中國頂級大學 "); //創(chuàng)建 學院 OrganizationComponent computerCollege = new College("計算機學院", " 計算機學院 "); OrganizationComponent infoEngineercollege = new College("信息工程學院", " 信息工程學院 "); //創(chuàng)建各個學院下面的系(專業(yè)) computerCollege.add(new Department("軟件工程", " 軟件工程不錯 ")); computerCollege.add(new Department("網(wǎng)絡工程", " 網(wǎng)絡工程不錯 ")); computerCollege.add(new Department("計算機科學與技術", " 計算機科學與技術是老牌的專業(yè) ")); infoEngineercollege.add(new Department("通信工程", " 通信工程不好學 ")); infoEngineercollege.add(new Department("信息工程", " 信息工程好學 ")); //將學院加入到 學校 university.add(computerCollege); university.add(infoEngineercollege); //university.print(); infoEngineercollege.print(); } }
UML類圖
總結(jié)
組合模式可以簡化客戶端操作??蛻舳酥恍枰鎸σ恢碌膶ο蠖挥每紤]整體部分或者節(jié)點葉子的問題。
具有較強的擴展性。當我們要更改組合對象時,我們只需要調(diào)整內(nèi)部的層次關系,客戶端不用做出任何改動。
方便創(chuàng)建出復雜的層次結(jié)構。客戶端不用理會組合里面的組成細節(jié),容易添加節(jié)點或者葉子從而創(chuàng)建出復雜的樹形結(jié)構。
需要遍歷組織機構,或者處理的對象具有樹形結(jié)構時, 非常適合使用組合模式.
要求較高的抽象性,如果節(jié)點和葉子有很多差異性的話,比如很多方法和屬性都不一樣,不適合使用組合模式。
Java 的集合類-HashMap 就使用了組合模式。
到此這篇關于Java結(jié)構型設計模式之組合模式Composite Pattern詳解的文章就介紹到這了,更多相關Java Composite Pattern內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Java實現(xiàn)百萬Excel數(shù)據(jù)導出
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)百萬Excel數(shù)據(jù)導出,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下2024-03-03SpringBoot+Swagger-ui自動生成API文檔
今天小編就為大家分享一篇關于SpringBoot+Swagger-ui自動生成API文檔,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Spring Security 構建rest服務實現(xiàn)rememberme 記住我功能
這篇文章主要介紹了Spring Security 構建rest服務實現(xiàn)rememberme 記住我功能,需要的朋友可以參考下2018-03-03Android bdflow數(shù)據(jù)庫神器的使用
這篇文章主要介紹了Android bdflow數(shù)據(jù)庫神器的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03JAVA隨機數(shù)隨機字母的實現(xiàn)(微信搶紅包小練習)
這篇文章主要介紹了JAVA隨機數(shù)隨機字母的實現(xiàn)(微信搶紅包小練習),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04