CTFSHOW-日刷-[萌新賽]萌新記憶/sql盲注
阿新 • • 發佈:2021-10-07
組合模式基本介紹
1.組合模式又叫部分整體模式,他建立了物件組的樹形結構,將物件組合成樹狀結構表示“整體-部分”的層次關係
2.組合模式依據樹形結構來組合物件,用來表示部分以及整體層次
3.組合能讓客戶以一致的方式處理個別對象以及組合物件
組合模式的基本類圖
原理結構圖說明
1.Component:這是組合中的物件宣告介面,在適當情況下,實現所有類共有的介面預設行為,用於訪問和管理Component 子部件,Component可以是抽象類或者抽象介面 2.Leaf:在組合中表示葉子節點,葉子節點沒有位元組點 3.Composite:非葉子節點,用於儲存子部件,在Component介面中實現子部件的相關操作,比如add、remove等
組合模式解決的問題
1.組合模式解決這樣的問題,當我們要處理的物件可以生成一顆樹形結構,而我們要對樹上的葉子節點進行操作時,它能夠提供一致的方式,而不用考慮它是節點還是葉子
組合模式解決學校院系展示
編寫程式展示一個學校的院系結構:要在一個頁面中展示出學校的院系組成,一個學校有多個學院,一個學院有多個系
思路圖解
程式碼實現
package com.cedric.composite; public abstract class OrganizationComponent { private String name;// 名字 private String des;// 說明 protected void add(OrganizationComponent organizationComponent){ // 預設實現 throw new UnsupportedOperationException(); } protected void remove(OrganizationComponent organizationComponent){ // 預設實現 throw new UnsupportedOperationException(); } public OrganizationComponent(String name,String des){ 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,抽象 protected abstract void print(); }
package com.cedric.composite; 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) { organizationComponentList.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() + "==============="); // 便利List for(OrganizationComponent organizationComponent : organizationComponentList){ organizationComponent.print(); } } }
package com.cedric.composite;
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) {
// 實際業務中,College的add和University的add不一定完全一樣
organizationComponentList.add(organizationComponent);
}
//重寫remove方法
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponentList.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() + "===============");
// 便利List
for(OrganizationComponent organizationComponent : organizationComponentList){
organizationComponent.print();
}
}
}
package com.cedric.composite;
public class Department extends OrganizationComponent{
public Department(String name, String des) {
super(name, des);
}
// add 和 remove 不需要重寫了,屬於葉子節點
@Override
public String getDes() {
return super.getDes();
}
@Override
public String getName() {
return super.getName();
}
@Override
protected void print() {
System.out.println(getName());
}
}
package com.cedric.composite;
public class Client {
public static void main(String[] args) {
// 從大到小建立物件 學校
OrganizationComponent university = new University("清華大學","中國頂尖大學");
//建立學院
OrganizationComponent computerCollege = new College("計算機學院","計算機學院");
OrganizationComponent infoEngineerCollege = new College("資訊工程學院","資訊工程學院");
// 建立各個學院下面的系
computerCollege.add(new Department("軟體工程","軟體工程"));
computerCollege.add(new Department("網路工程","網路工程"));
computerCollege.add(new Department("計算機技術與科學","計算機技術與科學"));
infoEngineerCollege.add(new Department("通訊工程","難度大"));
infoEngineerCollege.add(new Department("資訊工程","不太難"));
// 將學院加入到學校
university.add(computerCollege);
university.add(infoEngineerCollege);
computerCollege.print();
}
}
組合模式的注意事項和細節
1.簡化客戶端操作,客戶端只需要面對一致的物件而不用考慮整體部分或葉子節點的問題 2.具有較強的擴充套件性,當我們要更改組合物件時,我們只需要調整內部的層次關係,客戶端不用做出任何改動 3.方便創建出複雜的層次結構。客戶端不用理會組合裡面的組成細節,容易新增節點或者葉子從而創建出複雜的樹形結構 4.需要遍歷組織結構,或者處理的物件具有樹形結構時,非常適合組合模式 5.要求較高的抽象性,如果節點和葉子有很多差異性的話,比如很多方法和屬性都不一樣,不適合使用組合模式