디자인 모드 의 장식 (Decorator) 모드 코드 상세 설명

3556 단어 Decorator
/**

 * Created with IntelliJ IDEA.

 * User: HYY

 * Date: 13-10-27

 * Time:   10:49

 * To change this template use File | Settings | File Templates.

 */

interface Product {

    public double money();

}



class Coffee implements Product{



    @Override

    public double money() {

        return 10;

    }

}



class Ice implements Product {



    private Product product;



    public Ice(Product product) {

        this.product = product;

    }



    @Override

    public double money() {

        return this.product.money() + 2;//     

    }

}



class Chocolate implements Product {

    private Product product;



    public Chocolate(Product product) {

        this.product = product;

    }



    @Override

    public double money() {

        return this.product.money() + 4;//     

    }

}



public class BuyCoffee {

    public static void main(String[] args) {

        Product coffee = new Coffee();

        Product iceCoffee = new Ice(coffee);

        Product chocolateIceCoffee = new Chocolate(iceCoffee);

        System.out.println("          ,  :"+chocolateIceCoffee.money()+"  。");

    }

}

좋은 웹페이지 즐겨찾기