23種設計模式(10)java組合模式
23種設計模式第四篇:java組合模式
介紹
組合模式又叫做部分-整體模式,它使我們樹型結構的問題中,模糊了簡單元素和復雜元素的概念,客戶程序可以向處理簡單元素一樣來處理復雜元素,從而使得客戶程序與復雜元素的內(nèi)部結構解藕.
組合模式可以優(yōu)化處理遞歸或分級數(shù)據(jù)結構.有許多關于分級數(shù)據(jù)結構的例子,使得組合模式非常有用武之地.
類圖
組成部分:
Component: 為參加組合的對象聲明一個公共接口, 不管是組合還是葉結點.
Leaf: 在組合中表示葉子結點對象,葉子結點沒有子結點.
Composite: 表示參加組合的有子對象的對象, 并給出樹枝購件的行為.
實例
FolderComponent
public abstract class FolderComponent { private String name; public String getName() { return name; } public void setName(final String name) { this.name = name; } public FolderComponent() { } public FolderComponent(final String name) { this.name = name; } public abstract void add(FolderComponent component); public abstract void remove(FolderComponent component); public abstract void display(); }
FileLeaf
public class FileLeaf extends FolderComponent { public FileLeaf(final String name) { super(name); } @Override public void add(final FolderComponent component) { // ... } @Override public void remove(final FolderComponent component) { // ... } @Override public void display() { System.out.println("FileLeaf:" + this.getName()); } }
FolderComposite
public class FolderComposite extends FolderComponent { private final List<FolderComponent> components; public FolderComposite(final String name) { super(name); this.components = new ArrayList<FolderComponent>(); } public FolderComposite() { this.components = new ArrayList<FolderComponent>(); } @Override public void add(final FolderComponent component) { this.components.add(component); } @Override public void remove(final FolderComponent component) { this.components.remove(component); } @Override public void display() { System.out.println("FolderComposite---name:" + this.getName()); for (final FolderComponent component : components) { System.out.println("FolderComposite---component-name:" + component.getName()); } } }
Client
public class Client { public static void main(final String[] args) { final FolderComponent leaf = new FileLeaf("runnable file"); leaf.display(); final FolderComponent folder = new FolderComposite("new folder"); folder.add(new FileLeaf("content1 in new folder")); folder.add(new FileLeaf("content2 in new folder")); folder.display(); } }
輸出結果:
FileLeaf:runnable file
FolderComposite---name:new folder
FolderComposite---component-name:content1 in new folder
FolderComposite---component-name:content2 in new folder
使用場景
以下情況下適用Composite模式:
1、你想表示對象的部分-整體層次結構
2、你希望用戶忽略組合對象與單個對象的不同,用戶將統(tǒng)一地使用組合結構中的所有對象。
總結
組合模式解耦了客戶程序與復雜元素內(nèi)部結構,從而使客戶程序可以向處理簡單元素一樣來處理復雜元素。
如果你想要創(chuàng)建層次結構,并可以在其中以相同的方式對待所有元素,那么組合模式就是最理想的選擇。本章使用了一個文件
系統(tǒng)的例子來舉例說明了組合模式的用途。在這個例子中,文件和目錄都執(zhí)行相同的接口,這是組合模式的關鍵。通過執(zhí)行相同的接口,你就可以用相同的方式對待文件和目錄,從而實現(xiàn)將文件或者目錄儲存為目錄的子級元素。
轉自:java知音
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Mybatis-Plus saveBatch()批量保存失效的解決
本文主要介紹了Mybatis-Plus saveBatch()批量保存失效的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01druid多數(shù)據(jù)源配置+Datasurce動態(tài)切換方式
這篇文章主要介紹了druid多數(shù)據(jù)源配置+Datasurce動態(tài)切換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot使用druid配置多數(shù)據(jù)源問題
這篇文章主要介紹了SpringBoot使用druid配置多數(shù)據(jù)源問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03