Composite模式

[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]
//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();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值