以下内容来自http://www.lifevv.com
本文介绍设计模式中的组合(Composite)模式的概念,用法,以及实际应用中怎么样使用组合模式进行开发。
Composite模式是构造型的设计模式之一,通过递归手段来构造诸如文件系统之类的树形的对象结构;Composite模式所代表的数据构造是一群具有统一接口界面的对象集合,并可以通过一个对象来访问所有的对象(遍历)。
Composite模式的类图描述:
[出自:wikimedia.org]
Component
树形结构的节点抽象
- 为所有的对象定义统一的接口(公共属性,行为等的定义)
- 提供管理子节点对象的接口方法
- [可选]提供管理父节点对象的接口方法
Leaf
树形结构的叶节点。Component的实现子类
Composite
树形结构的枝节点。Component的实现子类
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>