Java設計模式之組合模式深入刨析
1.基本介紹
1)組合模式(Composite Pattern),又叫部分整體模式,它創(chuàng)建了對象組的樹形結構,將對象組合成樹狀結構以表示“整體-部分”的層次關系
2)組合模式依據(jù)樹形結構來組合對象,用來表示部分以及整體層次
3)這種類型的設計模式屬于結構型模式
4)組合模式使得用戶對單個對象和組合對象的訪問具有一致性,即:組合能讓客戶以一致的方式處理個別對象以及組合對象
2.結構
組合模式主要包含三種角色:
- 抽象根節(jié)點(Component):定義系統(tǒng)各層次對象的共有方法和屬性,可以預定義一些默認行為和屬性
- 樹枝節(jié)點(Composite):定義樹枝節(jié)點的行為,存儲子節(jié)點,組合樹枝節(jié)點和葉子節(jié)點形成一個樹形結構
- 葉子節(jié)點(Leaf):葉子節(jié)點對象,其下再無分支,是系統(tǒng)層次遍歷的最小單位
3.組合模式解決的問題
1)組合模式解決這樣的問題,當我們要處理的對象可以生成一顆樹形結構,而我們要對樹上的節(jié)點和葉子進行操作時,它能夠提供一致的方式,而不用考慮它是節(jié)點還是葉子
2)對應的示意圖

4.組合模式解決學校院系展示
1)應用實例要求
編寫程序展示一個學校院系結構:需求是這樣的,要在一個頁面中展示出學校的院系組成,一個學校有多個學院,一個學院有多個系
2)思路分析和圖解(類圖)

3)代碼實現(xiàn)
Component組合對象聲明接口
package com.zte;
public abstract class OrganizationComponent {
private String name;// 名字
private String des;// 說明
public String getName() {
return name;
}
public String getDes() {
return 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) {
this.name = name;
this.des = des;
}
// 方法print,抽象方法
protected abstract void print();
}Composite非葉子節(jié)點
package com.zte;
import java.util.ArrayList;
import java.util.List;
// University 就是 Composite,可以管理College
public class University extends OrganizationComponent {
List<OrganizationComponent> organizationComponentList = new ArrayList<>();
// 構造器
public University(String name, String des) {
super(name, des);
}
//重寫add
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponentList.add(organizationComponent);
}
// 重寫remove
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponent.remove(organizationComponent);
}
@Override
protected void print() {
System.out.println("==========" + getName() + "=========");
for (OrganizationComponent organizationComponent : organizationComponentList) {
organizationComponent.print();
}
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
}Composite非葉子節(jié)點
package com.zte;
import java.util.ArrayList;
import java.util.List;
public class College extends OrganizationComponent {
// list中存放department
List<OrganizationComponent> organizationComponentList = new ArrayList<>();
public College(String name, String des) {
super(name, des);
}
//重寫add
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponentList.add(organizationComponent);
}
// 重寫remove
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponent.remove(organizationComponent);
}
@Override
protected void print() {
System.out.println("==========" + getName() + "=========");
for (OrganizationComponent organizationComponent : organizationComponentList) {
organizationComponent.print();
}
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
}Leaf葉子節(jié)點
package com.zte;
public class Department extends OrganizationComponent {
public Department(String name, String des) {
super(name, des);
}
// add和remove方法就不需要再寫了
@Override
protected void print() {
System.out.println("===========" + getName() + "=========");
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
}package com.zte;
public class Client {
public static void main(String[] args) {
// 創(chuàng)建大學
OrganizationComponent university = new University("清華大學", "中國最好的大學");
// 創(chuàng)建學院
OrganizationComponent college1 = new College("計算機學院", "計算機學院");
OrganizationComponent college2 = new College("信息工程學院", "信息工程學院");
// 創(chuàng)建各個學院下面的系
college1.add(new Department("軟件工程", "軟件工程"));
college1.add(new Department("網絡工程", "網絡工程"));
college1.add(new Department("計算機科學與技術", "老牌專業(yè)"));
college2.add(new Department("通信工程", "通信工程"));
college2.add(new Department("信息工程", "信息工程"));
// 將學院添加到學校中
university.add(college1);
university.add(college2);
// 打印大學底下的所有院系
university.print();
// 打印學院底下的所有系
college1.print();
}
}5.組合模式的注意事項和細節(jié)
1)簡化客戶端操作,客戶端只需要面對一致的對象而不用考慮整體部分或者節(jié)點葉子的問題
2)其有較強的擴展性,當我們需要修改組合對象時,我們只需要調整內部的層次關系,客戶端不用做出任何改動
3)方便創(chuàng)建出復雜的層次結構,客戶端不用理會組合里面的組成細節(jié),容易添加節(jié)點或者葉子從而創(chuàng)建出復雜的樹形結構
4)需要遍歷組織機構,或者處理的對象具有樹形結構時,非常適合使用組合模式
5)要求較高的抽象性,如果節(jié)點和葉子有很多差異性的話,比如很多方法和屬性都不一樣,不適合使用組合模式
到此這篇關于Java設計模式之組合模式深入刨析的文章就介紹到這了,更多相關Java組合模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatisPlus 實體類與數(shù)據(jù)庫表映射關系詳解
這篇文章主要介紹了mybatisPlus 實體類與數(shù)據(jù)庫表映射關系詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01

