Estaba mirando this artículo de la wikipedia, y no podía entender cómo diablos estaba funcionando. Un poco frustrado por no poder entender el código solo mirándolo, decidí portar el código a C# (soy .net, lo siento muchachos :)). Solo se necesitaban algunas modificaciones menores (hereda y extiende, base para súper, etc.) y ejecuta la aplicación. Para mi sorpresa, me dieron el siguiente resultado:Decorador Pregunta patrón C#/java
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
Sólo por curiosidad, puede cualquier dev java dime lo que es diferente aquí y por qué funciona el ejemplo wikipedia (si funciona como dicen que hace, por supuesto).
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Coffee sampleCoffee = new SimpleCoffee();
Console.WriteLine("Cost: " + sampleCoffee.getCost() + " Ingredient: " + sampleCoffee.getIngredient());
sampleCoffee = new Milk(sampleCoffee);
Console.WriteLine("Cost: " + sampleCoffee.getCost() + " Ingredient: " + sampleCoffee.getIngredient());
sampleCoffee = new Sprinkles(sampleCoffee);
Console.WriteLine("Cost: " + sampleCoffee.getCost() + " Ingredient: " + sampleCoffee.getIngredient());
sampleCoffee = new Whip(sampleCoffee);
Console.WriteLine("Cost: " + sampleCoffee.getCost() + " Ingredient: " + sampleCoffee.getIngredient());
Console.ReadKey();
}
}
//The Coffee Interface defines the functionality of Coffee implemented by decorator
public interface Coffee
{
double getCost(); // returns the cost of coffee
String getIngredient(); //returns the ingredients mixed with coffee
}
//implementation of simple coffee without any extra ingredients
public class SimpleCoffee : Coffee
{
double cost;
String ingredient;
public SimpleCoffee()
{
cost = 1;
ingredient = "Coffee";
}
public double getCost()
{
return cost;
}
public String getIngredient()
{
return ingredient;
}
}
//abstract decorator class - note that it implements coffee interface
abstract public class CoffeeDecorator : Coffee
{
protected Coffee decoratedCoffee;
protected String ingredientSeparator;
public CoffeeDecorator(Coffee decoratedCoffee)
{
this.decoratedCoffee = decoratedCoffee;
ingredientSeparator = ", ";
}
public CoffeeDecorator()
{
}
public double getCost() //note it implements the getCost function defined in interface Coffee
{
return decoratedCoffee.getCost();
}
public String getIngredient()
{
return decoratedCoffee.getIngredient();
}
}
//Decorator Milk that mixes milk with coffee
//note it extends CoffeeDecorator
public class Milk : CoffeeDecorator
{
double cost;
String ingredient;
public Milk(Coffee decoratedCoffee) : base(decoratedCoffee)
{
cost = 0.5;
ingredient = "Milk";
}
public double getCost()
{
return base.getCost() + cost;
}
public String getIngredient()
{
return base.getIngredient() + base.ingredientSeparator + ingredient;
}
}
//Decorator Whip that mixes whip with coffee
//note it extends CoffeeDecorator
public class Whip : CoffeeDecorator
{
double cost;
String ingredient;
public Whip(Coffee decoratedCoffee)
: base(decoratedCoffee)
{
cost = 0.7;
ingredient = "Whip";
}
public double getCost()
{
return base.getCost() + cost;
}
public String getIngredient()
{
return base.getIngredient() + base.ingredientSeparator + ingredient;
}
}
//Decorator Sprinkles that mixes sprinkles with coffee
//note it extends CoffeeDecorator
public class Sprinkles : CoffeeDecorator
{
double cost;
String ingredient;
public Sprinkles(Coffee decoratedCoffee) : base(decoratedCoffee)
{
cost = 0.2;
ingredient = "Sprinkles";
}
public double getCost()
{
return base.getCost() + cost;
}
public String getIngredient()
{
return base.getIngredient() + base.ingredientSeparator + ingredient;
}
}
}
No sé C#. Pero parece que no anulas tus métodos. –
aquí el artículo de msdn sobre http://msdn.microsoft.com/en-us/library/ms173152%28VS.80%29.aspx –