Composite比較輕易理解,想到Composite就應該想到樹形結構圖。組合體內這些對象都有共同接口,當組合體一個對象的方法被調用執行時,Composite將遍歷(Iterator)整個樹形結構,尋找同樣包含這個方法的對象并實現調用執行??梢杂脿恳粍影賮硇稳荨?br /> 所以Composite模式使用到Iterator模式,和Chain of Responsibility模式類似。
public class Disk extends Equipment { public Disk(String name) { super(name); } //定義Disk實價為1 public double netPrice() { return 1.; } //定義了disk折扣價格是0.5 對折。 public double discountPrice() { return .5; } }