I was studying the "Decorator pattern". Doing some tests with C# I do not understand why I do not get the expected result. This is the code:
public abstract class Drink
{
public string description = "Generic drink";
public string GetDescription()
{
return description;
}
}
public abstract class DrinkDecorator : Drink
{
public abstract string GetDescription();
}
public class SecretIngredient : DrinkDecorator
{
Drink drink;
public SecretIngredient (Drink drink)
{
this.drink = drink;
}
public override string GetDescription()
{
return this.drink.GetDescription() + ", SecretIngredient ";
}
}
public class Espresso : Drink
{
public Espresso()
{
description = "Espresso";
}
}
[TestFixture]
class TestClass
{
[Test]
public void TestMethod()
{
Drink drink = new Espresso();
System.Diagnostics.Debug.WriteLine(drink.GetDescription());
drink = new SecretIngredient (drink);
System.Diagnostics.Debug.WriteLine(drink.GetDescription());
}
}
Performing the test I get:
Espresso
Generic drink
While I would have expected:
Espresso
Espresso, SecretIngredient
Why? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire