[size=large]Composite模式主要包含三个部分:[/size]
[b]1. Component接口:[/b]为Client对象调用提供的接口,也是Composie模式要管理的数据结构的统称。
[b]2. Leaf:[/b]Composite模式所管理的数据结构中的单个实体。
[b]3. Composite:[/b]Composite模式所管理的数据结构中的集合实体。
[size=large]Composite模式主要有以下几个方面的作用:[/size]
1. 作为接口模式为系统提供扩展功能。
2. 提供数据管理功能,所管理的数据结构通常为树结构,拥有统一的父接口。为父接口的不同实现提供不同的行为,并提供数据结构的递归查询。
[size=large][b]UML图:[/b][/size]
[align=center][img]http://f-1.tuzhan.com:8080/p-2/l/2012/08/05/14/57062284f2814ae7bfadd1d5644d9def.jpg[/img][/align]
[size=large][b]示例代码:[/b][/size]
[b]1. Component接口:[/b]为Client对象调用提供的接口,也是Composie模式要管理的数据结构的统称。
[b]2. Leaf:[/b]Composite模式所管理的数据结构中的单个实体。
[b]3. Composite:[/b]Composite模式所管理的数据结构中的集合实体。
[size=large]Composite模式主要有以下几个方面的作用:[/size]
1. 作为接口模式为系统提供扩展功能。
2. 提供数据管理功能,所管理的数据结构通常为树结构,拥有统一的父接口。为父接口的不同实现提供不同的行为,并提供数据结构的递归查询。
[size=large][b]UML图:[/b][/size]
[align=center][img]http://f-1.tuzhan.com:8080/p-2/l/2012/08/05/14/57062284f2814ae7bfadd1d5644d9def.jpg[/img][/align]
[size=large][b]示例代码:[/b][/size]
//Component
public interface Component{
public void operation();
}
//Leaf
public class Leaf implements Component{
public void operation(){
//TODO what you want the leaf to do.
}
}
//Composite
public class Composite implements Component{
private List<Component> components;
public void operation(){
//递归循环
for(Component component : components){
component.operation();
}
}
}