合成(Composite)模式

合成模式有时又叫做部分-整体模式(Part-Whole)。合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式可以使客户端将单纯元素与复合元素同等看待。

合成模式的实现根据所实现接口的区别分为两种形式,分别称为安全模式和透明模式。

透明方式

作为第一种选择,在Component里面声明所有的用来管理子类对象的方法,包括add()、remove(),以及getChild()方法。这样做的好处是所有的构件类都有相同的接口。在客户端看来,树叶类对象与合成类对象的区别起码在接口层次上消失了,客户端可以同等同的对待所有的对象。这就是透明形式的合成模式。

这个选择的缺点是不够安全,因为树叶类对象和合成类对象在本质上是有区别的。树叶类对象不可能有下一个层次的对象,因此add()、remove()以及getChild()方法没有意义,是在编译时期不会出错,而只会在运行时期才会出错。

安全方式

第二种选择是在Composite类里面声明所有的用来管理子类对象的方法。这样的做法是安全的做法,因为树叶类型的对象根本就没有管理子类对象的方法,因此,如果客户端对树叶类对象使用这些方法时,程序会在编译时期出错。

这个选择的缺点是不够透明,因为树叶类和合成类将具有不同的接口。

这两个形式各有优缺点,需要根据软件的具体情况做出取舍决定。

 

一,安全式结构

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

抽象构件(Component)角色:这是一个抽象角色,它给参加组合的对象定义出公共的接口及其默认行为,可以用来管理所有的子对象。在安全式的合成模式里,构件角色并不是定义出管理子对象的方法,这一定义由树枝构件对象给出。

树叶构件(Leaf)角色:树叶对象是没有下级子对象的对象,定义出参加组合的原始对象的行为。

树枝构件(Composite)角色:代表参加组合的有下级子对象的对象。树枝对象给出所有的管理子对象的方法,如add()、remove()、getChild()等。

二,安全式示例代码

import java.util.ArrayList;

/**
 * 抽象构件
 * @author Salmon
 *
 */
public abstract class Component {
	protected String name;

	public Component(String name) {
		this.name = name;
	}

	public abstract void display(int depth);
}

/**
 * 树枝构件
 * @author Salmon
 *
 */
public class Composite extends Component {

	private ArrayList<Component> children = new ArrayList<Component>();

	public Composite(String name) {
		super(name);
	}

	public void add(Component component) {
		children.add(component);
	}

	public void remove(Component component) {
		children.remove(component);
	}

	public void display(int depth) {
		for (Component component : children) {
			component.display(depth + 2);
		}
	}
}

/**
 * 树叶构件
 * @author Salmon
 *
 */
public class Leaf extends Component {
	public Leaf(String name) {
		super(name);
	}

	public void display(int depth) {
		System.out.println("");
	}
}

/**
 * 客户端代码
 * @author Salmon
 *
 */
public class Client {
	public void main() {
		Composite root = new Composite("root");
		root.add(new Leaf("Leaf A"));
		root.add(new Leaf("Leaf B"));
		Composite comp = new Composite("Composite X");

		comp.add(new Leaf("Leaf XA"));
		comp.add(new Leaf("Leaf XB"));
		root.add(comp);

		root.add(new Leaf("Leaf C"));

		// Add and remove a leaf
		Leaf l = new Leaf("Leaf D");
		root.add(l);
		root.remove(l);

		// Recursively display nodes
		root.display(1);
	}
}

 

三,透明式结构

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

抽象构件(Component)角色:这是一个抽象角色,它给参加组合的对象规定一个接口,规范共有的接口及默认行为。

树叶构件(Leaf)角色:代表参加组合的树叶对象,定义出参加组合的原始对象的行为。树叶类会给出add()、remove()以及getChild()之类的用来管理子类对对象的方法的平庸实现。

树枝构件(Composite)角色:代表参加组合的有子对象的对象,定义出这样的对象的行为。

四,透明式示例代码

import java.util.ArrayList;

/**
 * 抽象构件
 * @author Salmon
 *
 */
public abstract class Component {
	protected String name;

	public Component(String name) {
		this.name = name;
	}

	public abstract void add(Component c);

	public abstract void remove(Component c);

	public abstract void display(int depth);
}

/**
 * 树枝构件
 * @author Salmon
 *
 */
public class Composite extends Component {
	private ArrayList<Component> children = new ArrayList<Component>();

	public Composite(String name) {
		super(name);
	}

	public void add(Component component) {
		children.add(component);
	}

	public void remove(Component component) {
		children.remove(component);
	}

	public void display(int depth) {
		System.out.println("");
		;

		// Display each of the node's children
		for (Component component : children)
			component.display(depth + 2);
	}
}

/**
 * 树叶构件
 * @author Salmon
 *
 */
class Leaf extends Component {
	public Leaf(String name) {
		super(name);
	}

	public void add(Component c) {
		System.out.println("Cannot add to a leaf");
	}

	public void remove(Component c) {
		System.out.println("Cannot remove from a leaf");
	}

	public void display(int depth) {
		System.out.println("");
	}
}

/**
 * 客户端代码
 * @author Salmon
 *
 */
public class Client {
	public static void main(String[] args) {
		// Create a tree structure
		Composite root = new Composite("root");
		root.add(new Leaf("Leaf A"));
		root.add(new Leaf("Leaf B"));
		Composite comp = new Composite("Composite X");
		comp.add(new Leaf("Leaf XA"));
		comp.add(new Leaf("Leaf XB"));
		root.add(comp);
		root.add(new Leaf("Leaf C"));
		Leaf l = new Leaf("Leaf D");
		root.add(l);
		root.remove(l);
		root.display(1);
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值