`

读《研磨设计模式》-代码笔记-组合模式

阅读更多
声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/




import java.util.ArrayList;
import java.util.List;

abstract class Component {
	
	public abstract void printStruct(String preStr);
	
	/*以下三个方法简单地抛出例外。叶子类Leaf不覆盖这些方法,表示不支持。而Composite则重写这些方法
	 * 这涉及到组合模式的透明性和安全性,就是说叶子类要不要知道这些方法
	 * 如果从安全性考虑,那叶子类不要知道这些方法,就不要在父类(Component)中定义
	 * 个人认为透明性更重要一些,可以将Leaf和Composite的操作统一起来
	 */
	public void addChild(Component child) {
		throw new UnsupportedOperationException("addChild():not allowed.");
	}
	
	public void removeChild(Component child) {
		throw new UnsupportedOperationException("removeChild():not allowed.");
	}
	
	public Component getChild(int index) {
		throw new UnsupportedOperationException("getChild():not allowed.");
	}
	
	/*书上提到两个扩展:
	 * 1.父组件引用。主要用在删除上:当子目录删除时,子目录下的元素上移一层(将这些元素的父目录设为子目录的上一层):
			private Component parent;
			private List<Component> children;
	 * 2.环状引用。要记录路径,以免重复
	 */
}

class Leaf extends Component {
	private String name;
	public Leaf(String name) {
		this.name = name;
	}
	@Override
	public void printStruct(String preStr) {
		System.out.println(preStr + "-" + name);
	}
}

class Composite extends Component {

	private String name;

	private List<Component> list;
	
	public Composite(String name) {
		this.name = name;
	}

	public void addChild(Component child) {
		if (list == null) {
			list = new ArrayList<Component>();
		}
		list.add(child);
	}
	
	public void removeChild(Component child) {
		if (list != null) {
			list.remove(child);
		}
	}
	
	@Override
	public void printStruct(String preStr) {
		System.out.println(preStr + "+" + name);
		if (list != null) {
			preStr += " ";
			for (Component child : list) {
				child.printStruct(preStr);
			}
		}
	}
	
	
}

public class CompositePattern {

	public static void main(String[] args) {
		Component root = new Composite("服装");
		Component c1 = new Composite("男装");
		Component c2 = new Composite("女装");

		// 定义所有的叶子对象
		Component leaf1 = new Leaf("衬衣");
		Component leaf2 = new Leaf("夹克");
		Component leaf3 = new Leaf("裙子");
		Component leaf4 = new Leaf("套装");

		// 按照树的结构来组合组合对象和叶子对象
		root.addChild(c1);
		root.addChild(c2);
		c1.addChild(leaf1);
		c1.addChild(leaf2);
		c2.addChild(leaf3);
		c2.addChild(leaf4);
		// 调用根对象的输出功能来输出整棵树
		root.printStruct("");
	}

}
1
8
分享到:
评论

相关推荐

    研磨设计模式-配套源代码

    研磨设计模式-配套源代码研磨设计模式-配套源代码

    研磨设计模式-配套源代码.rar

    研磨设计模式-配套源代码.rar

    研磨设计模式-配套源代码.7z

    研磨设计模式-配套源代码.7z

    研磨设计模式-配套源代码 UTF-8格式

    研磨设计模式第二版-配套源代码 UTF-8格式 研磨设计模式第二版-配套源代码 UTF-8格式

    研磨设计模式-陈臣.epub

    “1.1 设计模式是什么 1.1.1 什么是模式 从字面上理解,模,就是模型、模板的意思;式,就是方式、方法的意思。综合起来,所谓模式就是:可以作为模型或模板的方式或方法。... “研磨设计模式”。 iBooks.

    研磨设计模式-陈臣.王斌.扫描高清版PDF

    设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。 使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;...

    研磨设计模式-陈臣pdf

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式书-配套源代码(全)

    1:本源代码是《研磨设计模式》一书的配套源代码 2:每个模式的示例源代码放在一个单独的文件夹下,以该模式的英文名称命名 3:每个模式下分成多个example,按照书的示例顺序分别命名为example1、example2.........

    研磨设计模式-陈臣.mobi kindle版

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式--chjavach的博客文章

    单例模式、工厂方法模式、策略模式、命令模式和桥接模式。

    研磨设计模式 源代码

    研磨设计模式 源代码 书上面的例子都在里面

    研磨设计模式源码

    研磨设计模式的配套源码,请下载,不要想了,解压后,直接放到eclipse下面即可运行!

    研磨设计模式-part2

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式-part4

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式-part3

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式全部源代码

    研磨设计模式全部源代码,个人收集,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!

    研磨设计模式带书签完整版228M.7z.001

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式(完整带书签).part2.pdf

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式 完美书签 完整(一)

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式PDF

    研磨设计模式PDF

Global site tag (gtag.js) - Google Analytics