打字猴:1.700468646e+09
1700468646
1700468647 @Deprecated
1700468648
1700468649 public ArrayList<Component>getChildren()throws UnsupportedOperationException{
1700468650
1700468651 //空实现
1700468652
1700468653 throw new UnsupportedOperationException();
1700468654
1700468655 }
1700468656
1700468657 }
1700468658
1700468659 为什么要加个Deprecated注解呢?就是在编译器期告诉调用者,你可以调我这个方法,但是可能出现错误哦,我已经告诉你“该方法已经失效”了,你还使用那在运行期也会抛出UnsupportedOperationException异常。
1700468660
1700468661 在透明模式下,遍历整个树形结构是比较容易的,不用进行强制类型转换,如代码清单21-24所示。
1700468662
1700468663 代码清单21-24 树结构遍历
1700468664
1700468665 public class Client{
1700468666
1700468667 //通过递归遍历树
1700468668
1700468669 public static void display(Component root){
1700468670
1700468671 for(Component c:root.getChildren()){
1700468672
1700468673 if(c instanceof Leaf){//叶子节点
1700468674
1700468675 c.doSomething();
1700468676
1700468677 }else{//树枝节点
1700468678
1700468679 display(c);
1700468680
1700468681 }
1700468682
1700468683 }
1700468684
1700468685 }
1700468686
1700468687 }
1700468688
1700468689 仅仅在遍历时不再进行牵制的类型转化了,其他的组装则没有任何变化。透明模式的好处就是它基本遵循了依赖倒转原则,方便系统进行扩展。
1700468690
1700468691
1700468692
1700468693
1700468694 设计模式之禅 21.4.3 组合模式的遍历
1700468695
[ 上一页 ]  [ :1.700468646e+09 ]  [ 下一页 ]