【設計模式】Java設計模式 – 組合模式
Java設計模式 – 組合模式
😄 不斷學習才是王道
🔥 繼續踏上學習之路,學之分享筆記
👊 總有一天我也能像各位大佬一樣
🏆原創作品,更多關注我CSDN: 一個有夢有戲的人
👊準備將部落格園、CSDN一起記錄分享自己的學習心得!!!
🌝分享學習心得,歡迎指正,大家一起學習成長!
簡介
組合模式(Composite Pattern),又叫部分整體模式,是用於把一組相似的對象當作一個單一的對象。組合模式依據樹形結構來組合對象,用來表示部分以及整體層次。這種類型的設計模式屬於結構型模式,它創建了對象組的樹形結構。
組合模式原理
首先先看一下組合的UML圖
Compoent:抽象類或者介面,是組合對象聲明的介面,實現所有類的默認行為,用於訪問、管理子部件。
Leaf:組合中的葉子節點,最小的類
Composite:非葉子節點,用來操作組合對象,存儲子部件。
組合模式實例
接下來用一個例子來學習組合模式,學校有學院,學院下有專業,這就是一層一層的關係,需要在一個頁面中展現出那個學校有什麼學院,學院下有什麼專業。
來看一下例子的類圖
①、定義抽象類-Component
定義屬性,構造器,getset,操作方法需要默認實現,因為在葉子節點是不需要去實現的,如果定義成抽象類,子類就必須實現了。在定義一個抽象類-列印資訊。
package com.lyd.demo.composite;
/**
* @Author: lyd
* @Description: 抽象類
* @Date: 2022-08-30
*/
public abstract class OrganizationComponent {
private String name;
private String description;
public OrganizationComponent(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
// 添加 - 子類不一定需要實現
public void add(OrganizationComponent organizationComponent) {
throw new UnsupportedOperationException();
}
// 刪除 - 子類不一定需要實現
public void remove(OrganizationComponent organizationComponent) {
throw new UnsupportedOperationException();
}
// 列印 - 子類必須去實現
public abstract void print();
}
②、定義葉子類和非葉子類
大學類:非葉子,組合 院系類;定義一個數組來存放組合對象,通過重寫操作方法對其進行操作。
package com.lyd.demo.compose;
import com.lyd.demo.composite.OrganizationComponent;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: lyd
* @Description: 學校類 - 繼承OrganizationComponent - 組合 院系類
* @Date: 2022-08-30
*/
public class University extends OrganizationComponent {
// 組合 College 類
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public University(String name, String description) {
super(name, description);
}
/**
* 重寫add
*/
@Override
public void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
/**
* 重寫remove
*/
@Override
public void remove(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
// 列印包含學院的資訊
@Override
public void print() {
System.out.println("< " + getName() + " >");
// 將所有學院資訊列印出來
for (OrganizationComponent o : organizationComponents) {
o.print();
}
}
}
學院類:非葉子,組合Department類,定義一個數組來存放組合對象,通過重寫操作方法對其進行操作。
package com.lyd.demo.compose;
import com.lyd.demo.composite.OrganizationComponent;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: lyd
* @Description: 學院類
* @Date: 2022-08-30
*/
public class College extends OrganizationComponent {
// 組合 Department 類
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public College(String name, String description) {
super(name, description);
}
/**
* 重寫add
*/
@Override
public void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
/**
* 重寫remove
*/
@Override
public void remove(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
// 列印包含學院的資訊
@Override
public void print() {
System.out.println("< " + getName() + " >");
// 將所有專業資訊列印出來
for (OrganizationComponent o : organizationComponents) {
o.print();
}
}
}
專業類:葉子節點,沒有組合的集合,所以不需要進行操作,只需要進行輸出列印。
package com.lyd.demo.compose;
import com.lyd.demo.composite.OrganizationComponent;
/**
* @Author: lyd
* @Description: 專業類
* @Date: 2022-08-30
*/
public class Department extends OrganizationComponent {
// 已經沒有集合了
public Department(String name, String description) {
super(name, description);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
// 葉子節點,就不需要add和remove
@Override
public void print() {
System.out.println(getName());
}
}
③、測試
package com.lyd.demo.test;
import com.lyd.demo.compose.College;
import com.lyd.demo.compose.Department;
import com.lyd.demo.compose.University;
import com.lyd.demo.composite.OrganizationComponent;
/**
* @Author: lyd
* @Description: 測試
* @Date: 2022-08-30
*/
public class ComposeTest {
public static void main(String[] args) {
// 創建大學
OrganizationComponent ZheJiangUniversity = new University("浙江大學", "人才之地");
// 創建學院
OrganizationComponent ComputerCollege = new College("電腦科學與技術學院", "-->電腦科學與技術學院");
OrganizationComponent OpticalCollege = new College("光電科學與工程學院", "-->光電科學與工程學院");
// 創建專業
Department ComputerDepartment = new Department("電腦科學與技術", "--電腦科學與技術");
Department SoftWareDepartment = new Department("軟體工程", "--軟體工程");
Department OpticalDepartment = new Department("光電資訊科學與工程", "--光電資訊科學與工程");
// 添加專業
ComputerCollege.add(ComputerDepartment);
ComputerCollege.add(SoftWareDepartment);
OpticalCollege.add(OpticalDepartment);
// 添加學院
ZheJiangUniversity.add(ComputerCollege);
ZheJiangUniversity.add(OpticalCollege);
// 列印所有
ZheJiangUniversity.print();
System.out.println("*******************************");
// 列印學院
ComputerCollege.print();
System.out.println("*******************************");
// 列印院系
ComputerDepartment.print();
}
}
組合可以理解成是層層相套。
運行結果:
通俗的講,組合模式就是將對象組合到非類中,在非子類中進行對他們的操作,有種層層相套的感覺,可以通過打斷點的形式一步一步了解。
👍創作不易,可能有些語言不是很通暢,如有錯誤請指正,感謝觀看!記得一鍵三連哦!👍