ITeye博客

Composite Pattern(组合模式)

futrueboy · 2009-05-13 20:28 · Design Pattern(设计模式) · Windows · XML · 设计模式 · Markdown

Composite Pattern(组合模式)

定义:

组合多个对象形成属性结构以表示整体-部分的结构层次,组合模式对单个对象和组合对象的使用具有一致性。

 

 

应用场景:

  1. 你想表示一个对象整体或部分层次。
  2. 你想让客户能够忽略不同对象的层次的变化。
  3. 对象的结构是动态的并且复杂程度不一样,但客户需要一致地处理它们。

 

实例:

 

 

结果输出:

My Computer
--C:\
----word2007.doc
----powerpoint2007.ppt
----windows
------system32
--------regedit.exe
--D:\
----xml2007.xml

 

 

代码分析:

1. 输出递归目录

/***********************************************************************
 * Module:  ClientApp.java
 * Author:  hongqin
 * Purpose: Defines the Class ClientApp
 ***********************************************************************/

import java.util.*;

public class ClientApp {

	/*
	 * print the tree
	 */
   public int print(File file, int level) {
	   print(level);
	   System.out.println(file.getFileName());
	   
	   if (file instanceof Directory) {
		   level ++;
		   Iterator<File> children = ((Directory)file).getIteratorFile();
		   File next;
		   while (children.hasNext())
		   {
			   next = children.next();
			   if (next instanceof Document) {
				   print(level);
				   System.out.println(next.getFileName());
			   } else {
				   print(next, level);
			   }
		   }
	   }
      return 0;
   }
   
   /*
    * print the symbols
    */
   public void print(int level) {
	   for (int i = 0; i < level; i++) {
		   System.out.print("--");
	   }
   }

}

2. 目录文件

/***********************************************************************
 * Module:  Directory.java
 * Author:  hongqin
 * Purpose: Defines the Class Directory
 ***********************************************************************/

import java.util.*;

public class Directory implements File {
	private String fileName;
	public List<File> fileList;

	// print file name
	public Directory(String string) {
		this.fileName = string;
	}

	public String getFileName() {
		return this.fileName;
	}

	/** @pdGenerated default getter */
	public List<File> getFile() {
		if (fileList == null)
			fileList = new ArrayList<File>();
		return fileList;
	}

	/** @pdGenerated default iterator getter */
	public Iterator<File> getIteratorFile() {
		if (fileList == null)
			fileList = new ArrayList<File>();
		return fileList.iterator();
	}

	/**
	 * @pdGenerated default setter
	 * @param newFile
	 */
	public void setFile(List<File> newFile) {
		removeAllFile();
		for (Iterator<File> iter = newFile.iterator(); iter.hasNext();)
			addFile((File) iter.next());
	}

	/**
	 * @pdGenerated default add
	 * @param newFile
	 */
	public void addFile(File newFile) {
		if (newFile == null)
			return;
		if (this.fileList == null)
			this.fileList = new ArrayList<File>();
		if (!this.fileList.contains(newFile))
			this.fileList.add(newFile);
	}

	/**
	 * @pdGenerated default remove
	 * @param oldFile
	 */
	public void removeFile(File oldFile) {
		if (oldFile == null)
			return;
		if (this.fileList != null)
			if (this.fileList.contains(oldFile))
				this.fileList.remove(oldFile);
	}

	/** @pdGenerated default removeAll */
	public void removeAllFile() {
		if (fileList != null)
			fileList.clear();
	}

}

3. 其他详见源代码包中

 

 

 

 

注:

  1. 定义以及应用场景出自《深入浅出设计模式》莫勇腾
  2. 使用软件:PowerDesigner12.5

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。