最近在看《设计模式之禅》这本书,收获良多,作者不愧是工作多年的大牛,将各种设计模式讲解的非常透彻。这里备份下书中的【 组合模式 】代码:
github:
【 https://github.com/cstriker1407/design_pattern 】
代码如下:
abstract class Crop { private String name; private List<Crop> cropList = new ArrayList<Crop>(); public Crop(String name) { super(); this.name = name; } public List<Crop> getCropList() { return cropList; } public Crop addCrop(Crop crop) { this.cropList.add(crop); return this; } public abstract String oper(); public void info() { 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 (Crop crop : cropList) { crop.info(); } } } } class Leaf extends Crop { public Leaf(String name) { super(name); } @Override public String oper() { return "LeafOper"; } } class Leader extends Crop { public Leader(String arg0) { super(arg0); } @Override public String oper() { return "LeaderOper"; } }
测试代码:
public class CompositeTest { public static void test() { Leader A01 = new Leader("A01"); Leader A11 = new Leader("A11"); Leader A12 = new Leader("A12"); Leaf B13 = new Leaf("B13"); A01.addCrop(A11).addCrop(A12).addCrop(B13); Leaf B21 = new Leaf("B21"); Leaf B22 = new Leaf("B22"); Leaf B23 = new Leaf("B23"); A11.addCrop(B21); A12.addCrop(B22).addCrop(B23); A01.info(); } }
发表评论