本文節選自《設計模式就該這樣學》
1 使用透明組合模式實作課程目錄結構
以一門網路課程為例,我們設計一個課程的關系結構,比如,我們有Java入門課程、人工智能課程、Java設計模式、原始碼分析、軟技能等,而Java設計模式、原始碼分析、軟技能又屬于Java架構師系列課程包,每個課程的定價都不一樣,但是,這些課程不論怎么組合,都有一些共性,而且是整體和部分的關系,可以用組合模式來設計,首先創建一個頂層的抽象組件CourseComponent類,
/**
* Created by Tom.
*/
public abstract class CourseComponent {
public void addChild(CourseComponent catalogComponent){
throw new UnsupportedOperationException("不支持添加操作");
}
public void removeChild(CourseComponent catalogComponent){
throw new UnsupportedOperationException("不支持洗掉操作");
}
public String getName(CourseComponent catalogComponent){
throw new UnsupportedOperationException("不支持獲取名稱操作");
}
public double getPrice(CourseComponent catalogComponent){
throw new UnsupportedOperationException("不支持獲取價格操作");
}
public void print(){
throw new UnsupportedOperationException("不支持列印操作");
}
}
把所有可能用到的方法都定義到這個頂層的抽象組件中,但是不寫任何邏輯處理的代碼,而是直接拋例外,這里,有些小伙伴會有疑惑,為什么不用抽象方法?因為用了抽象方法,其子類就必須實作,這樣便體現不出各子類的細微差異,所以子類繼承此抽象類后,只需要重寫有差異的方法覆寫父類的方法即可,
然后分別創建課程Course類和課程包CoursePackage類,創建Course類的代碼如下,
/**
* Created by Tom.
*/
public class Course extends CourseComponent {
private String name;
private double price;
public Course(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String getName(CourseComponent catalogComponent) {
return this.name;
}
@Override
public double getPrice(CourseComponent catalogComponent) {
return this.price;
}
@Override
public void print() {
System.out.println(name + " (¥" + price + "元)");
}
}
創建CoursePackage類的代碼如下,
/**
* Created by Tom.
*/
public class CoursePackage extends CourseComponent {
private List<CourseComponent> items = new ArrayList<CourseComponent>();
private String name;
private Integer level;
public CoursePackage(String name, Integer level) {
this.name = name;
this.level = level;
}
@Override
public void addChild(CourseComponent catalogComponent) {
items.add(catalogComponent);
}
@Override
public String getName(CourseComponent catalogComponent) {
return this.name;
}
@Override
public void removeChild(CourseComponent catalogComponent) {
items.remove(catalogComponent);
}
@Override
public void print() {
System.out.println(this.name);
for(CourseComponent catalogComponent : items){
//控制顯示格式
if(this.level != null){
for(int i = 0; i < this.level; i ++){
//列印空格控制格式
System.out.print(" ");
}
for(int i = 0; i < this.level; i ++){
//每一行開始列印一個+號
if(i == 0){ System.out.print("+"); }
System.out.print("-");
}
}
//列印標題
catalogComponent.print();
}
}
}
最后撰寫客戶端測驗代碼,
public static void main(String[] args) {
System.out.println("============透明組合模式===========");
CourseComponent javaBase = new Course("Java入門課程",8280);
CourseComponent ai = new Course("人工智能",5000);
CourseComponent packageCourse = new CoursePackage("Java架構師課程",2);
CourseComponent design = new Course("Java設計模式",1500);
CourseComponent source = new Course("原始碼分析",2000);
CourseComponent softSkill = new Course("軟技能",3000);
packageCourse.addChild(design);
packageCourse.addChild(source);
packageCourse.addChild(softSkill);
CourseComponent catalog = new CoursePackage("課程主目錄",1);
catalog.addChild(javaBase);
catalog.addChild(ai);
catalog.addChild(packageCourse);
catalog.print();
}
運行結果如下圖所示,

透明組合模式把所有公共方法都定義在 Component 中,這樣客戶端就不需要區分操作物件是葉子節點還是樹枝節點;但是,葉子節點會繼承一些它不需要(管理子類操作的方法)的方法,這與設計模式的介面隔離原則相違背,
2 使用安全組合模式實作無限級檔案系統
再舉一個程式員更熟悉的例子,對于程式員來說,電腦是每天都要接觸的,電腦的檔案系統其實就是一個典型的樹形結構,目錄包含檔案夾和檔案,檔案夾里面又可以包含檔案夾和檔案,下面用代碼來實作一個目錄系統,
檔案系統有兩個大的層次:檔案夾和檔案,其中,檔案夾能容納其他層次,為樹枝節點;檔案是最小單位,為葉子節點,由于目錄系統層次較少,且樹枝節點(檔案夾)結構相對穩定,而檔案其實可以有很多型別,所以我們選擇使用安全組合模式來實作目錄系統,可以避免為葉子節點型別(檔案)引入冗余方法,首先創建頂層的抽象組件Directory類,
public abstract class Directory {
protected String name;
public Directory(String name) {
this.name = name;
}
public abstract void show();
}
然后分別創建File類和Folder類,創建File類的代碼如下,
public class File extends Directory {
public File(String name) {
super(name);
}
@Override
public void show() {
System.out.println(this.name);
}
}
創建Folder類的代碼如下,
import java.util.ArrayList;
import java.util.List;
public class Folder extends Directory {
private List<Directory> dirs;
private Integer level;
public Folder(String name,Integer level) {
super(name);
this.level = level;
this.dirs = new ArrayList<Directory>();
}
@Override
public void show() {
System.out.println(this.name);
for (Directory dir : this.dirs) {
//控制顯示格式
if(this.level != null){
for(int i = 0; i < this.level; i ++){
//列印空格控制格式
System.out.print(" ");
}
for(int i = 0; i < this.level; i ++){
//每一行開始列印一個+號
if(i == 0){ System.out.print("+"); }
System.out.print("-");
}
}
//列印名稱
dir.show();
}
}
public boolean add(Directory dir) {
return this.dirs.add(dir);
}
public boolean remove(Directory dir) {
return this.dirs.remove(dir);
}
public Directory get(int index) {
return this.dirs.get(index);
}
public void list(){
for (Directory dir : this.dirs) {
System.out.println(dir.name);
}
}
}
注意,Folder類不僅覆寫了頂層的show()方法,還增加了list()方法,
最后撰寫客戶端測驗代碼,
public static void main(String[] args) {
System.out.println("============安全組合模式===========");
File qq = new File("QQ.exe");
File wx = new File("微信.exe");
Folder office = new Folder("辦公軟體",2);
File word = new File("Word.exe");
File ppt = new File("PowerPoint.exe");
File excel = new File("Excel.exe");
office.add(word);
office.add(ppt);
office.add(excel);
Folder wps = new Folder("金山軟體",3);
wps.add(new File("WPS.exe"));
office.add(wps);
Folder root = new Folder("根目錄",1);
root.add(qq);
root.add(wx);
root.add(office);
System.out.println("----------show()方法效果-----------");
root.show();
System.out.println("----------list()方法效果-----------");
root.list();
}
運行結果如下圖所示,

安全組合模式的好處是介面定義職責清晰,符合設計模式的單一職責原則和介面隔離原則;缺點是客戶需要區分樹枝節點和葉子節點,這樣才能正確處理各個層次的操作,客戶端無法依賴抽象介面(Component),違背了設計模式的依賴倒置原則,
【推薦】Tom彈架構:收藏本文,相當于收藏一本“設計模式”的書
本文為“Tom彈架構”原創,轉載請注明出處,技術在于分享,我分享我快樂!
如果本文對您有幫助,歡迎關注和點贊;如果您有任何建議也可留言評論或私信,您的支持是我堅持創作的動力,關注微信公眾號『 Tom彈架構 』可獲取更多技術干貨!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/348147.html
標籤:Java
上一篇:java PC 和 手機 在線考試系統 模塊設計方案
下一篇:Java 在PPT中創建散點圖
