关于装饰者模式的层层调用

    学习装饰者模式的时候,关于装饰者的层层调用觉得很是不明白。想到了好多,因为以前没这样写过。

Beverage beverage = new DarkRoast();
//就是来搞明白怎么回事。怎么层层调用
beverage = new Whip(beverage);
beverage = new Soy(beverage);
beverage = new Mocha(beverage);

    实在看书没想明白,于是写了一遍代码,才发现被我忽略了一部分代码。不过还是写一下记录记录。

    //Soy类  代码

public class Soy extends CondimentDecorator {
   Beverage beverage;

    public Soy(Beverage beverage){
        this.beverage = beverage;//beverage=Mocha
    }
    @Override
    public String getDescription() {        
        return beverage.getDescription() +" , Soy";
    }
    public double cost(){
        System.out.println("Soy cost"+" "+beverage.toString());
        return 0.15 + beverage.cost();
    }         
}

//Whip类  代码

public class Whip extends CondimentDecorator {
   Beverage beverage;

    public Whip(Beverage beverage){
        this.beverage = beverage; //beverage = DarkRoast
    }
    @Override
    public String getDescription() {
    
        return beverage.getDescription() +" , Whip";
        
    }
    public double cost(){
         System.out.println("Whip post"+" "+beverage.toString());
         
        return 0.10+beverage.cost();
    }
          
}

//Mocha类  代码

public class Mocha extends CondimentDecorator {
   Beverage beverage;

    public Mocha(Beverage beverage){
        this.beverage = beverage; //beverage =soy;
    }
    @Override
    public String getDescription() {         
        return beverage.getDescription() +" , Mocha";        
    }
    public double cost(){
        System.out.println("Mocha cost"+" "+beverage.toString());
        return 0.20+beverage.cost();
    }
          
}

//DarkRost

public class DarkRoast extends Beverage {
    public DarkRoast(){
         description  = "DarkRoast";
    }
    public double cost() {
        System.out.println("DarRoast cost");
        return 0.99;
    }
}

//Main

public class StarbuzzCoffee {

    public static void main(String[] args) {
        Beverage beverage = new DarkRoast();
        //就是来搞明白怎么回事。
        beverage = new Whip(beverage);
        beverage = new Soy(beverage);
        beverage = new Mocha(beverage);
        
       System.out.println(beverage.cost()+"  "+beverage.getDescription());
    }

}

然后再运行调试的时候注意到了

public Mocha(Beverage beverage){
        this.beverage = beverage; //beverage =soy;
    }

Mocha会存储上一个beverage对象。

在能用到的部分cost()方法都加上了 beverage.toString(),用来看看beverage对象名称。

输出结果:

Mocha cost 配料包.Soy@15db9742
Soy cost 配料包.Whip@6d06d69c
Whip post 饮料包.DarkRoast@7852e922
DarkRoast cost
1.44 DarkRoast , Whip , Soy , Mocha

在调用beverage.cost时。

         会去调用Mocha.cost方法,但是Mocha存储的beverage为Soy。然后Soy里存储的beverage为whip,一次类推。直到调用DarkRoast.cost方法。

这样就像装饰一样,从外面一层一层的添加包装。然后记录次里面的包装对象。在调用时,从外向内依次调用。

很简单的小问题,可惜在看书的时候没有转过弯来~

对外扩展开发,对修改关闭

Java I/O流也是大量使用了该模式,如下:

FileOutputStream fos  =   new  FileOutputStream( " dos.txt " );   //  获得写入文件功能 
 BufferedOutputStream bos  =   new  BufferedOutputStream(fos);  //  获得缓冲功能   

DataOutputStream dos  =   new  DataOutputStream(bos);   //  获得写入基本类型功能

 

posted @ 2016-04-07 18:32  夜の魅  阅读(549)  评论(0编辑  收藏  举报