Vous êtes sur la page 1sur 2

DecoratorPattern

class Program
{
static void Main(string[] args)
{
Pizza largePizza = new LargePizza();
largePizza = new Cheese(largePizza);
largePizza = new Ham(largePizza);
largePizza = new Peppers(largePizza);
Console.WriteLine(largePizza.GetDescription());
Console.WriteLine("{0:C2}", largePizza.CalculateCost());
Console.ReadKey();
}
}

public abstract class Pizza


{
public string Description { get; set; }
public abstract string GetDescription();
public abstract double CalculateCost();
}

public class LargePizza : Pizza


{
public LargePizza()
{
Description = "Large Pizza";
}
public override string GetDescription()
{
return Description;
}
public override double CalculateCost()
{
return 9.00;
}
}

public class PizzaDecorator : Pizza


{
protected Pizza _pizza;
public PizzaDecorator(Pizza pizza)
{
_pizza = pizza;
}
public override string GetDescription()
{
return _pizza.Description;
}
public override double CalculateCost()
{
return _pizza.CalculateCost();
}
}

public class Cheese : PizzaDecorator


{
public Cheese(Pizza pizza)
: base(pizza)
{
Description = "Cheese";
}
public override string GetDescription()
{
return String.Format("{0}, {1}", _pizza.GetDescription(), Description);
}
public override double CalculateCost()
{
return _pizza.CalculateCost() + 1.25;
}
}
public class Ham : PizzaDecorator
{
public Ham(Pizza pizza)
: base(pizza)
{
Description = "Ham";
}

public override string GetDescription()


{
return String.Format("{0}, {1}", _pizza.GetDescription(), Description);
}

public override double CalculateCost()


{
return _pizza.CalculateCost() + 1.00;
}
}