Attached is a classic Decorator pattern. My question is how would you modify the below code so that you can wrap zero or one of each topping on to the Pizza
Right now I can have a Pepporini -> Sausage --> Pepporini --> Pizza class driving the total cost up to $10, charging twice for Pepporini.
I don't think I want to use the Chain of Responsibility pattern as order does not matter and not all toppings are used?
Thank you
namespace PizzaDecorator
{
public interface IPizza
{
double CalculateCost();
}
public class Pizza: IPizza
{
public Pizza()
{
}
public double CalculateCost()
{
return 8.00;
}
}
public abstract class Topping : IPizza
{
protected IPizza _pizzaItem;
public Topping(IPizza pizzaItem)
{
this._pizzaItem = pizzaItem;
}
public abstract double CalculateCost();
}
public class Pepporini : Topping
{
public Pepporini(IPizza pizzaItem)
: base(pizzaItem)
{
}
public override double CalculateCost()
{
return this._pizzaItem.CalculateCost() + 0.50;
}
}
public class Sausage : Topping
{
public Sausage(IPizza pizzaItem)
: base(pizzaItem)
{
}
public override double CalculateCost()
{
return this._pizzaItem.CalculateCost() + 1.00;
}
}
public class Onions : Topping
{
public Onions(IPizza pizzaItem)
: base(pizzaItem)
{
}
public override double CalculateCost()
{
return this._pizzaItem.CalculateCost() + .25;
}
}
}
Right now I can have a Pepporini -> Sausage --> Pepporini --> Pizza class driving the total cost up to $10, charging twice for Pepporini.
I don't think I want to use the Chain of Responsibility pattern as order does not matter and not all toppings are used?
Thank you
namespace PizzaDecorator
{
public interface IPizza
{
double CalculateCost();
}
public class Pizza: IPizza
{
public Pizza()
{
}
public double CalculateCost()
{
return 8.00;
}
}
public abstract class Topping : IPizza
{
protected IPizza _pizzaItem;
public Topping(IPizza pizzaItem)
{
this._pizzaItem = pizzaItem;
}
public abstract double CalculateCost();
}
public class Pepporini : Topping
{
public Pepporini(IPizza pizzaItem)
: base(pizzaItem)
{
}
public override double CalculateCost()
{
return this._pizzaItem.CalculateCost() + 0.50;
}
}
public class Sausage : Topping
{
public Sausage(IPizza pizzaItem)
: base(pizzaItem)
{
}
public override double CalculateCost()
{
return this._pizzaItem.CalculateCost() + 1.00;
}
}
public class Onions : Topping
{
public Onions(IPizza pizzaItem)
: base(pizzaItem)
{
}
public override double CalculateCost()
{
return this._pizzaItem.CalculateCost() + .25;
}
}
}