面向物件:組合(Composite)模式
阿新 • • 發佈:2019-01-25
以下內容來自http://www.lifevv.com
本文介紹設計模式中的組合(Composite)模式的概念,用法,以及實際應用中怎麼樣使用組合模式進行開發。
Composite模式的概念
Composite模式是構造型的設計模式之一,通過遞迴手段來構造諸如檔案系統之類的樹形的物件結構;Composite模式所代表的資料構造是一群具有統一介面介面的物件集合,並可以通過一個物件來訪問所有的物件(遍歷)。
Composite模式的類圖描述:
[出自:wikimedia.org]
Component
樹形結構的節點抽象
- 為所有的物件定義統一的介面(公共屬性,行為等的定義)
- 提供管理子節點物件的介面方法
- [可選]提供管理父節點物件的介面方法
Leaf
樹形結構的葉節點。Component的實現子類
Composite
樹形結構的枝節點。Component的實現子類
Composite模式的應用場景
Composite模式概念起來比簡單,簡單一點說,可以使用Composite模式來構造一個具有統一介面介面的樹形的物件群,並可通過該介面訪問物件群的每個物件。
我們應用Composite模式來實現檔案系統的檔案/目錄結構:
IFile:File與Folder的共通介面介面。相當於Component。
Folder:目錄。目錄下面有子目錄,檔案。相當於Composite。
File:檔案。存在於目錄之中。相當於Leaf。
Client類:測試類或者說使用類。
程式碼:
- import java.util.ArrayList;
- import java.util.List;
- public class Client {
- public static void main(String[] args) {
- //構造一個樹形的檔案/目錄結構
- Folder rootFolder = new Folder( "c://" );
- Folder compositeFolder = new Folder( "composite" );
- rootFolder.addChild(compositeFolder);
- Folder windowsFolder = new Folder( "windows" );
- rootFolder.addChild(windowsFolder);
- File file = new File( "TestComposite.java" );
- compositeFolder.addChild(file);
- //從rootFolder訪問整個物件群
- printTree(rootFolder);
- }
- private static void printTree(IFile ifile) {
- ifile.printName();
- List <IFile> children = ifile.getChildren();
- for (IFile file:children) {
- if (file instanceof File) {
- System.out.print(" " );
- file.printName();
- } else if (file instanceof Folder) {
- printTree(file);
- }
- }
- }
- }
- interface IFile {
- public void printName();
- public boolean addChild(IFile file);
- public boolean removeChild(IFile file);
- public List<IFile> getChildren();
- }
- class File implements IFile {
- private String name;
- public File(String name) {
- this .name = name;
- }
- public void printName() {
- System.out.println(name);
- }
- public boolean addChild(IFile file) {
- return false ;
- }
- public boolean removeChild(IFile file) {
- return false ;
- }
- public List<IFile> getChildren() {
- return null ;
- }
- }
- class Folder implements IFile {
- private String name;
- private List <IFile> childList;
- public Folder(String name) {
- this .name = name;
- this .childList = new ArrayList<IFile>();
- }
- public void printName() {
- System.out.println(name);
- }
- public boolean addChild(IFile file) {
- return childList.add(file);
- }
- public boolean removeChild(IFile file) {
- return childList.remove(file);
- }
- public List<IFile> getChildren() {
- return childList;
- }
- }
import java.util.ArrayList;
import java.util.List;
public class Client {
public static void main(String[] args) {
//構造一個樹形的檔案/目錄結構
Folder rootFolder = new Folder("c://");
Folder compositeFolder = new Folder("composite");
rootFolder.addChild(compositeFolder);
Folder windowsFolder = new Folder("windows");
rootFolder.addChild(windowsFolder);
File file = new File("TestComposite.java");
compositeFolder.addChild(file);
//從rootFolder訪問整個物件群
printTree(rootFolder);
}
private static void printTree(IFile ifile) {
ifile.printName();
List <IFile> children = ifile.getChildren();
for (IFile file:children) {
if (file instanceof File) {
System.out.print(" ");
file.printName();
} else if (file instanceof Folder) {
printTree(file);
}
}
}
}
interface IFile {
public void printName();
public boolean addChild(IFile file);
public boolean removeChild(IFile file);
public List<IFile> getChildren();
}
class File implements IFile {
private String name;
public File(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
public boolean addChild(IFile file) {
return false;
}
public boolean removeChild(IFile file) {
return false;
}
public List<IFile> getChildren() {
return null;
}
}
class Folder implements IFile {
private String name;
private List <IFile> childList;
public Folder(String name) {
this.name = name;
this.childList = new ArrayList<IFile>();
}
public void printName() {
System.out.println(name);
}
public boolean addChild(IFile file) {
return childList.add(file);
}
public boolean removeChild(IFile file) {
return childList.remove(file);
}
public List<IFile> getChildren() {
return childList;
}
}
執行Client,輸出結果:
C:/Composite>javac *.java
C:/Composite>java Client
c:/
composite
TestComposite.java
windows
C:/Composite>