dimanche 1 février 2015

Head First Design Patterns - Decorator Pattern using Starbuzz

I am going through the book Head First Design Patterns and am specifically looking at the Starbuzz example for the Decorator pattern.


I am having trouble understanding that what exactly is the need for CondimentDecorator in the example provided. Why can't Mocha simply extend Beverage? What's the need for another layer of abstraction?`



public abstract class Beverage
{
String description = "Unknown beverage";

public String getDescription()
{
return description;
}

public abstract double cost();
}

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

public class Mocha extends CondimentDecorator
{
Beverage b;

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

public String getDescription()
{
return b.getDescription() + ", Mocha";
}

public double cost()
{
return .20 + b.cost();
}
}

Aucun commentaire:

Enregistrer un commentaire