1700468460
1700468461
*/
1700468462
1700468463
}
1700468464
1700468465
场景类负责树状结构的建立,并可以通过递归方式遍历整个树,如代码清单21-21所示。
1700468466
1700468467
代码清单21-21 场景类
1700468468
1700468469
public class Client{
1700468470
1700468471
public static void main(String[]args){
1700468472
1700468473
//创建一个根节点
1700468474
1700468475
Composite root=new Composite();
1700468476
1700468477
root.doSomething();
1700468478
1700468479
//创建一个树枝构件
1700468480
1700468481
Composite branch=new Composite();
1700468482
1700468483
//创建一个叶子节点
1700468484
1700468485
Leaf leaf=new Leaf();
1700468486
1700468487
//建立整体
1700468488
1700468489
root.add(branch);
1700468490
1700468491
branch.add(leaf);
1700468492
1700468493
}
1700468494
1700468495
//通过递归遍历树
1700468496
1700468497
public static void display(Composite root){
1700468498
1700468499
for(Component c:root.getChildren()){
1700468500
1700468501
if(c instanceof Leaf){//叶子节点
1700468502
1700468503
c.doSomething();
1700468504
1700468505
}else{//树枝节点
1700468506
1700468507
display((Composite)c);
1700468508
1700468509
}
[
上一页 ]
[ :1.70046846e+09 ]
[
下一页 ]