基本介紹
組合模式,又叫部分整體模式,它創建了對象組的樹形結構,將對象組合成樹狀結構以表示“整體-部分”的層次關系。
組合模式依據樹形結構來組合對象,用來表示部分以及整體層次。
這種類型的設計模式屬於結構性模式。
組合模式使得用戶對單個對象和組合對象的訪問具有一致性,即:組合能讓客戶以一致的方式處理個別對象以及組合對象。
組合模式原理類圖:
- Component:這是組合中對象聲明接口,在適當情況下,實現所有類共有的接口默認行為,用於訪問和管理Component子部件,Component可以是抽象類或接口。
- Leaf:在組合中表示葉子節點,葉子節點沒有子節點。
- Composite:非葉子節點,用於存儲子部件,在Component接口中實現子部件的相關操作,比如增加、刪除。
傳統方式
案例
編寫程序展示一個學校院系結構,需求是這樣的:要在一個頁面中展示出學校的院系組成,一個學校有多個學院,一個學院有多個系。如圖:
傳統方案解決學校院系展示
分析:
- 將學院看作是學校的子類,系是學院的子類,這樣實際上是站在組織大小來進行分層次的。
- 實際上我們的要求是:在一個頁面中展示出學校的院系組成,一個學校有多個學院,一個學院有多個系,因此這種方案,不能很好地實現管理的操作,比如對學院、系的添加、刪除、遍歷等。
- 解決方案:把學校、院、系都看作是組織結構 ,他們之間沒有繼承的關系,而是一個樹形結構,可以更好的實現管理操作(組合模式)
組合模式
思路分析
代碼實現
1public abstract class OrganizationComponent {
2
3 private String name;//名字
4
5 private String des;//描述
6
7 public OrganizationComponent(String name, String des) {
8 this.name = name;
9 this.des = des;
10 }
11
12 protected void add(OrganizationComponent organizationComponent) {
13 //默認實現
14 throw new UnsupportedOperationException();
15 }
16
17 protected void remove(OrganizationComponent organizationComponent) {
18 //默認實現
19 throw new UnsupportedOperationException();
20 }
21
22 protected abstract void print();
23
24
25 public String getName() {
26 return name;
27 }
28
29 public void setName(String name) {
30 this.name = name;
31 }
32
33 public String getDes() {
34 return des;
35 }
36
37 public void setDes(String des) {
38 this.des = des;
39 }
40}
1//University就是 Composite,可以管理College
2public class University extends OrganizationComponent {
3
4 List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
5
6 public University(String name, String des) {
7 super(name, des);
8 }
9
10 @Override//重寫
11 protected void add(OrganizationComponent organizationComponent) {
12 organizationComponents.add(organizationComponent);
13 }
14
15 @Override//重寫
16 protected void remove(OrganizationComponent organizationComponent) {
17 organizationComponents.remove(organizationComponent);
18 }
19
20 @Override//重寫
21 public String getName() {
22 return super.getName();
23 }
24
25 @Override//重寫
26 public String getDes() {
27 return super.getDes();
28 }
29
30 @Override
31 protected void print() {
32 System.out.println("---------------" + getName() + "-------------------");
33 for (OrganizationComponent organizationComponent : organizationComponents) {
34 organizationComponent.print();
35 }
36 }
37}
1public class College extends OrganizationComponent {
2
3 List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
4
5 public College(String name, String des) {
6 super(name, des);
7 }
8
9 @Override//重寫
10 protected void add(OrganizationComponent organizationComponent) {
11 organizationComponents.add(organizationComponent);
12 }
13
14 @Override//重寫
15 protected void remove(OrganizationComponent organizationComponent) {
16 organizationComponents.remove(organizationComponent);
17 }
18
19 @Override//重寫
20 public String getName() {
21 return super.getName();
22 }
23
24 @Override//重寫
25 public String getDes() {
26 return super.getDes();
27 }
28
29 @Override
30 protected void print() {
31 System.out.println("---------------" + getName() + "-------------------");
32 for (OrganizationComponent organizationComponent : organizationComponents) {
33 organizationComponent.print();
34 }
35 }
36}
1public class Depatment extends OrganizationComponent {
2
3 public Depatment(String name, String des) {
4 super(name, des);
5 }
6
7 @Override
8 protected void print() {
9 System.out.println(getName());
10 }
11
12 //add、remove不需要寫了,因為它是葉子節點,不需要管理其他節點
13
14 @Override
15 public String getDes() {
16 return super.getDes();
17 }
18
19 @Override
20 public String getName() {
21 return super.getName();
22 }
23}
1public class Client {
2 public static void main(String[] args) {
3
4 //從大到小創建對象,學校
5 OrganizationComponent university = new University("清華大學", "頂級大學");
6
7 //學院
8 OrganizationComponent college1 = new College("計算機學院", "計算機學院");
9 OrganizationComponent college2 = new College("信息工程學院", "信息工程學院");
10
11 //專業
12 college1.add(new Depatment("軟件工程", "軟件工程"));
13 college1.add(new Depatment("網絡工程", "網絡工程"));
14 college2.add(new Depatment("通信工程", "通信工程"));
15 college2.add(new Depatment("信息工程", "信息工程"));
16
17 //將學院加入到學校
18 university.add(college1);
19 university.add(college2);
20
21 university.print();
22 college1.print();
23
24 }
25}
注意事項
簡化客戶端操作。客戶端只需要面對一致的對象而不用考慮整體部分或者節點葉子的問題。
具有較強的擴展性。當我們要更改組合對象時,我們只需要調整內部的層次關系,客戶端不用做出任何改動。
方便創建出復雜的層次結構。客戶端不用理會組合里面的組成細節,容易添加節點或者葉子從而創建出復雜的樹形結構。
需要遍歷組織機構,或者處理的對象具有樹形結構時,非常適合使用組合模式。
要求較高的抽象性,如果節點和葉子有很多差異性的話,比如很多方法和屬性都不一樣,不適合使用組合模式。
