2010-06-24 46 views
3

这是一个关于代码行为的问题,而不是模式本身。我会制定出下面的代码首先设计模式:装饰行为令人费解

public abstract class Beverage { 
    protected String description; 

    public String getDescription(){ 
     return description; 
    } 
    public abstract BigDecimal cost(); 
} 


public abstract class CondimentDecorator extends Beverage{ 
    @Override 
    public abstract String getDescription(); 
} 

public class HouseBlend extends Beverage{ 

    public HouseBlend() { 
     description = "House Blend"; 
    } 

    @Override 
    public BigDecimal cost() { 
     return BigDecimal.valueOf(.89); 
    } 

} 

public class Mocha extends CondimentDecorator{ 
    Beverage beverage; 

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

    @Override 
    public String getDescription() { 
     System.out.println("desc: " + beverage.getDescription()); 
     return beverage.getDescription() + ", Mocha"; 
    } 

    @Override 
    public BigDecimal cost() { 
     System.out.println("bev: "+beverage.cost()); 
     return BigDecimal.valueOf(.20).add(beverage.cost()); 
    } 

} 

public class CoffeeTest { 
     public static void main(String args[]){ 
      Beverage blend = new HouseBlend(); 
      blend = new Mocha(blend); 
      blend = new Mocha(blend); 
      blend = new Mocha(blend); 
      System.out.println(blend.getDescription() + " * "+blend.cost()); 
     } 
} 

当CoffeeTest运行我得到下面的输出,我想了解

1 desc: House Blend 
2 desc: House Blend, Mocha 
3 desc: House Blend 
4 desc: House Blend, Mocha, Mocha 
5 desc: House Blend 
6 desc: House Blend, Mocha 
7 desc: House Blend 
8 bev: 0.89 
9 bev: 1.09 
10 bev: 0.89 
11 bev: 1.29 
12 bev: 0.89 
13 bev: 1.09 
14 bev: 0.89 
15 House Blend, Mocha, Mocha, Mocha * 1.49 

所以这是我的问题:

  1. 我的预期'desc'和'bev'将被打印3倍,为什么xtra行?
  2. 当没有明确的状态保存时,“House Blend,Mocha,Mocha”如何打印?
  3. 我对'成本'有同样的问题,beverage.cost()如何通过添加金额来保存状态。

我确信答案在于Beverage和CondimentDecorator之间的多态性。

+0

我没有在这里看到装饰模式的用法 – 2010-06-24 14:57:29

+3

为什么不呢?这是文档:饮料混合=新HouseBlend();混合=新摩卡(混合);混合=新摩卡(混合); – OlimilOops 2010-06-24 15:06:27

+0

您是否尝试清洁该项目? – JRL 2010-06-24 15:07:10

回答

4

当没有明确的状态保存时,'House Blend,Mocha,Mocha'是如何打印的?

您正在创建3个不同的对象。让我们称他们为a,b和c。因此,我们可以重写代码如下所示:

Beverage a = new HouseBlend(); 
Beverage b = new Mocha(a); 
Beverage c = new Mocha(b); 
System.out.println(c.getDescription() + " * "+c.cost()); 

这将做同样的事情,你的代码,但它更清楚,你面对的3级不同的对象。分配

blend = new Mocha(blend); 

不会替换该对象,但实际上会创建一个新对象,并简单地将引用混合修改为新对象。

当您在代码中调用blend.getDescription()时,您指的是对象c,它调用对象b的getDescription,该对象调用对象a的getDescription。对象a的getDescription()返回String“House Blend”。所以,对象b的getDescription()返回“House Blend,Mocha”。然后对象c的getDescription()返回“House Blend,Mocha,Mocha”。

getCost()非常类似。

0

在Mocha.getDescription中,您有System.out.println,并且在返回某些东西时,您也会在void main中进行打印。但尽管如此,我得到以下几点:
说明:房子混合
说明:房子混合,摩卡
说明:房子混合
BEV:0.89
BEV:1.09
BEV:0.89
众议院混合,摩卡,摩卡* 1.29
如果无效的主要你有三次新摩卡输出看起来像你

public static void main(String[] args) { 
     Beverage blend = new HouseBlend(); 
     blend = new Mocha(blend); 
     blend = new Mocha(blend); 
     blend = new Mocha(blend); 
     System.out.println(blend.getDescription() + " * "+blend.cost()); 
} 

装饰图案就像缠绕物体对象在这儿是这样的:
HouseBlend has_a(摩卡has_a(摩卡has_a(摩卡)))

0

在getDescription,你叫beverage.getDescription()两次,这两者的打印线。