1700468410
1700468411
代码清单21-19 树枝构件
1700468412
1700468413
public class Composite extends Component{
1700468414
1700468415
//构件容器
1700468416
1700468417
private ArrayList<Component>componentArrayList=new ArrayList<Component>();
1700468418
1700468419
//增加一个叶子构件或树枝构件
1700468420
1700468421
public void add(Component component){
1700468422
1700468423
this.componentArrayList.add(component);
1700468424
1700468425
}
1700468426
1700468427
//删除一个叶子构件或树枝构件
1700468428
1700468429
public void remove(Component component){
1700468430
1700468431
this.componentArrayList.remove(component);
1700468432
1700468433
}
1700468434
1700468435
//获得分支下的所有叶子构件和树枝构件
1700468436
1700468437
public ArrayList<Component>getChildren(){
1700468438
1700468439
return this.componentArrayList;
1700468440
1700468441
}
1700468442
1700468443
}
1700468444
1700468445
树叶节点是没有子下级对象的对象,定义参加组合的原始对象行为,其通用源代码如代码清单21-20所示。
1700468446
1700468447
代码清单21-20 树叶构件
1700468448
1700468449
public class Leaf extends Component{
1700468450
1700468451
/*
1700468452
1700468453
*可以覆写父类方法
1700468454
1700468455
*public void doSomething(){
1700468456
1700468457
*
1700468458
1700468459
*}
[
上一页 ]
[ :1.70046841e+09 ]
[
下一页 ]