【2013-12-17】设计模式学习笔记:组合模式

【2013-12-17】设计模式学习笔记:组合模式
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2013-12-17 标题设计模式学习笔记组合模式分类编程 / 设计模式 / java 标签设计模式·java·组合模式设计模式学习笔记组合模式最近在看《设计模式之禅》这本书收获良多作者不愧是工作多年的大牛将各种设计模式讲解的非常透彻。这里备份下书中的【 组合模式 】代码github【 https://github.com/cstriker1407/design_pattern 】代码如下abstractclassCrop{privateStringname;privateListCropcropListnewArrayListCrop();publicCrop(Stringname){super();this.namename;}publicListCropgetCropList(){returncropList;}publicCropaddCrop(Cropcrop){this.cropList.add(crop);returnthis;}publicabstractStringoper();publicvoidinfo(){if(cropList.isEmpty()){System.out.println(No Crop, I am the leaf: name oper());}else{System.out.println(I Have childeren, I am: name oper());for(Cropcrop:cropList){crop.info();}}}}classLeafextendsCrop{publicLeaf(Stringname){super(name);}OverridepublicStringoper(){returnLeafOper;}}classLeaderextendsCrop{publicLeader(Stringarg0){super(arg0);}OverridepublicStringoper(){returnLeaderOper;}}测试代码publicclassCompositeTest{publicstaticvoidtest(){LeaderA01newLeader(A01);LeaderA11newLeader(A11);LeaderA12newLeader(A12);LeafB13newLeaf(B13);A01.addCrop(A11).addCrop(A12).addCrop(B13);LeafB21newLeaf(B21);LeafB22newLeaf(B22);LeafB23newLeaf(B23);A11.addCrop(B21);A12.addCrop(B22).addCrop(B23);A01.info();}}