http://lavasoft.blog.51cto.com/62575/90824 转载
从原理图可见,File、Folder都可以同等看待苇IFile,为对象管理提供了极大的便利。
当然,树的概念不单单是文件文件夹的层次概念,只是因为这个很形象,实际中还有很多树的概念,比如组织机构,分类层次等等,都是逻辑上的概念,不管是物理上的还是逻辑上的,在Java里都是一样处理的。
二、实例
下面以一个逻辑树为例子,以上面的原理图为蓝本,看看如何实现并如何使用这个树,这个结构很简单,但是如何去使用树,遍历树、为我所用还是有一定难度的。
这里主要用到树的递归遍历,如何递归、如何控制遍历层级,如何将逻辑关系转换为(类似)物理关系,这些都是有相当难度的。
1、
/** * 抽象文件角色 */ public interface IFile { //返回自己的实例 IFile getComposite(); //某个商业方法 void sampleOperation(); //获取深度 int getDeep(); //设置深度 void setDeep(int x); }
2、
/** * 文件夹角色 */ public class Folder implements IFile { private String name; //文件名字 private int deep; //层级深度,根深度为0 private Vector<IFile> componentVector = new Vector<IFile>(); public Folder(String name) { this.name = name; } //返回自己的实例 public IFile getComposite() { return this; } //某个商业方法 public void sampleOperation() { System.out.println("执行了某个商业方法!"); } //增加一个文件或文件夹 public void add(IFile IFile) { componentVector.addElement(IFile); IFile.setDeep(this.deep + 1); } //删除一个文件或文件夹 public void remove(IFile IFile) { componentVector.removeElement(IFile); } //返回直接子文件(夹)集合 public Vector getAllComponent() { return componentVector; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDeep() { return deep; } public void setDeep(int deep) { this.deep = deep; } }
3、
/** * 文件 */ public class File implements IFile { private String name; //文件名字 private int deep; //层级深度 public File(String name) { this.name = name; } //返回自己的实例 public IFile getComposite() { return this; } //某个商业方法 public void sampleOperation() { System.out.println("执行了某个商业方法!"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDeep() { return deep; } public void setDeep(int deep) { this.deep = deep; } }
4、
/** * 遍历树的一个测试 */ public class Client { public static String indentChar = "\t"; //文件层次缩进字符 public static void main(String args[]) { new Client().test(); } /** * 客户端测试方法 */ public void test() { //根下文件及文件夹 Folder root = new Folder("树根"); Folder b1_1 = new Folder("1_枝1"); Folder b1_2 = new Folder("1_枝2"); Folder b1_3 = new Folder("1_枝3"); File l1_1 = new File("1_叶1"); File l1_2 = new File("1_叶2"); File l1_3 = new File("1_叶3"); //b1_2下的文件及文件夹 Folder b2_1 = new Folder("2_枝1"); Folder b2_2 = new Folder("2_枝2"); File l2_1 = new File("2_叶1"); //缔造树的层次关系(简单测试,没有重复添加的控制) root.add(b1_1); root.add(b1_2); root.add(l1_1); root.add(l1_2); b1_2.add(b2_1); b1_2.add(b2_2); b1_2.add(l2_1); root.add(l1_3); root.add(b1_3); //控制台打印树的层次 outTree(root); } public void outTree(Folder folder) { System.out.println(folder.getName()); iterateTree(folder); } /** * 遍历文件夹,输入文件树 * * @param folder */ public void iterateTree(Folder folder) { Vector<IFile> clist = folder.getAllComponent(); //todo:遍历之前可以对clist进行排序,这些都不是重点 for (Iterator<IFile> it = clist.iterator(); it.hasNext();) { IFile em = it.next(); if (em instanceof Folder) { Folder cm = (Folder) em; System.out.println(getIndents(em.getDeep()) + cm.getName()); iterateTree(cm); } else { System.out.println(getIndents(em.getDeep()) + ((File) em).getName()); } } } /** * 文件层次缩进字符串 * * @param x 缩进字符个数 * @return 缩进字符串 */ public static String getIndents(int x) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < x; i++) { sb.append(indentChar); } return sb.toString(); } }5、
上面所用的合成模式是安全合成模式,所谓的安全是指File与Folder中的方法不同。Folder有对聚集对象的管理,File没有。
合成模式在程序设计中有着广泛的应用,比如Dom4j、资源管理器、Java GUI容器层次图等等都是合成模式应用的典范。