2014年10月27日月曜日

開発環境

Head Firstデザインパターン ―頭とからだで覚えるデザインパターンの基本 (Eric Freeman 著、Elisabeth Freeman 著、Kathy Sierra 著、Bert Bates 著、佐藤 直生 監訳、木下 哲也 翻訳、有限会社 福龍興業 翻訳、オライリージャパン)の3章(Decoratorパターン: オブジェクトの装飾)、自分で考えてみよう(p.83)を解いてみる。

その他参考書籍

自分で考えてみよう(p.83)

コード(BBEdit, Emacs)

Sample83.java

public class Beverage {
    public double cost() {
        double result = 0.0;
        if (hasMilk()) {
            result += milkCost;
        }
        if (hasSoy()) {
            result += soyCost;
        }
        if (hasMocha()) {
            result += mochaCost;
        }
        if (hasWhip()) {
            result += whipCost;
        }
        return result;
    }
}
public class DarkRoast extends Beverage {
    public DarkRoast() {
        description = "最高級ダークロースト";
    }
    public double cost () {
        return 100 + super.cost();
    }
}

0 コメント:

コメントを投稿