First, this assignment is not legal:
Coffee coffee = new Mocha(new Coffee());
A Mocha
is not a Coffee
nor is there an implicit cast from a Mocha
to a Coffee
. To "remove" the decorator, you need to provide either a method or a cast to do so. So you could add an undecorate method to Mocha
:
public Coffee Undecorate() {
return (Coffee)decoratedItem;
}
Then you could say
Coffee coffee = new Mocha(new Coffee()).Undecorate();
Alternatively, you could provide an implicit cast operator in the Mocha
class:
public static implicit operator Coffee(Mocha m) {
return (Coffee)m.decoratedItem;
}
Then your line
Coffee coffee = new Mocha(new Coffee());
would be legal.
Now, your question suggests a potential misunderstanding of the design pattern (and, in fact, your implementation suggests one too). What you're trying to do is very smelly. The right way to go about using the decorator pattern is like so. Note that CoffeeDecorator
derives from Coffee
!
abstract class Item { public abstract decimal Cost(); }
class Coffee : Item { public override decimal Cost() { return 1.99m; } }
abstract class CoffeeDecorator : Coffee {
protected Coffee _coffee;
public CoffeeDecorator(Coffee coffee) { this._coffee = coffee; }
}
class Mocha : CoffeeDecorator {
public Mocha(Coffee coffee) : base(coffee) { }
public override decimal Cost() { return _coffee.Cost() + 2.79m; }
}
class CoffeeWithSugar : CoffeeDecorator {
public CoffeeWithSugar(Coffee coffee) : base(coffee) { }
public override decimal Cost() { return _coffee.Cost() + 0.50m; }
}
Then you can say:
Coffee coffee = new Mocha(new CoffeeWithSugar(new Coffee()));
Console.WriteLine(coffee.Cost()); // output: 5.28
Given this, what do you need to undecorate it for?