using System;
public class Cake
{
public virtual decimal Cost { get;set; }
public virtual string Name {get;set;}
public virtual int Calories{get;set;}
public virtual void Bake()
{
AddLiquidIngredients();
AddSolidIngredients();
MixCake();
}
private void MixCake()
{
//You could be selling not mixed cake?
Cost += 2;
}
private void AddLiquidIngredients()
{
//Imagine the cost of each ingredients + calories etc.
Cost += 1;
Calories += 100;
}
private void AddSolidIngredients()
{
//Imagine the cost of each ingredients + calories etc.
Cost += 2;
Calories += 150;
}
}
public class FrostedCake : Cake
{
private Cake cake;
public override decimal Cost
{
get{return cake.Cost;}
set{cake.Cost = value;}
}
public override int Calories
{
get{return cake.Calories;}
set{cake.Calories = value;}
}
public override string Name
{
get{return cake.Name;}
set{cake.Name = value;}
}
public FrostedCake(Cake cake)
{
this.cake = cake;
}
public override void Bake()
{
cake.Bake();
Frost();
}
private void Frost()
{
Name += " frosted";
Calories += 200;
Cost += 5;
}
}
public class Test
{
public static void Main()
{
Cake cake = new Cake();
cake.Name = "Super cake";
cake.Cost = 10;
//I want a cake to be frosted
cake = new FrostedCake(cake);
/*
The frosted cake is an "add-on" to the cake itself. A frosted cake is nothing without a cake.
But the cake can exist by itself.
By using the decorator pattern you can, at runtime, add functionality to your existing instance of a class.
*/
cake.Bake();
Console.WriteLine(cake.Name);
}
}