Skip to content

Commit

Permalink
Added condiment decorator pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Long Mai committed May 2, 2011
1 parent 83c7bed commit e1be047
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 39 deletions.
34 changes: 2 additions & 32 deletions src/StarbuzzCoffee/StarbuzzCoffee/Beverage.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@

using System;
namespace StarbuzzCoffee
{
abstract class Beverage
{
public abstract string Description { get; }
public abstract decimal Cost { get; }
protected decimal CondimentCost
public override string ToString()
{
get
{
Decimal condimentCost = 0;

if(milk)
condimentCost += milkCost;
if(soy)
condimentCost += soyCost;
if(mocha)
condimentCost += mochaCost;
if(whip)
condimentCost += whipCost;

return condimentCost;
}
return Description + " $" + Cost;
}

private bool milk = false;
private bool soy = false;
private bool mocha = false;
private bool whip = false;

private decimal milkCost = 0.49M;
private decimal soyCost = 0.25M;
private decimal mochaCost = 1.00M;
private decimal whipCost = 0.89M;

public bool Milk { get { return milk; } set { milk = value; } }
public bool Soy { get { return soy; } set { soy = value; } }
public bool Mocha { get { return mocha; } set { mocha = value; } }
public bool Whip { get { return whip; } set { whip = value; } }
}
}
13 changes: 13 additions & 0 deletions src/StarbuzzCoffee/StarbuzzCoffee/Condiments/CondimentDecorator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

namespace StarbuzzCoffee.Condiments
{
abstract class CondimentDecorator : Beverage
{
protected readonly Beverage decoratedBeverage;

protected CondimentDecorator(Beverage beverage)
{
this.decoratedBeverage = beverage;
}
}
}
20 changes: 20 additions & 0 deletions src/StarbuzzCoffee/StarbuzzCoffee/Condiments/Milk.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

namespace StarbuzzCoffee.Condiments
{
class Milk : CondimentDecorator
{
private readonly decimal cost = 0.49M;

public Milk(Beverage beverage) : base(beverage) { }

public override string Description
{
get { return base.decoratedBeverage.Description + ", milk"; }
}

public override decimal Cost
{
get { return base.decoratedBeverage.Cost + cost; }
}
}
}
20 changes: 20 additions & 0 deletions src/StarbuzzCoffee/StarbuzzCoffee/Condiments/Mocha.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

namespace StarbuzzCoffee.Condiments
{
class Mocha : CondimentDecorator
{
private readonly decimal cost = 1.00M;

public Mocha(Beverage beverage) : base(beverage) { }

public override string Description
{
get { return base.decoratedBeverage.Description + ", mocha"; }
}

public override decimal Cost
{
get { return base.decoratedBeverage.Cost + cost; }
}
}
}
20 changes: 20 additions & 0 deletions src/StarbuzzCoffee/StarbuzzCoffee/Condiments/Soy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

namespace StarbuzzCoffee.Condiments
{
class Soy : CondimentDecorator
{
private readonly decimal cost = 0.25M;

public Soy(Beverage beverage) : base(beverage) { }

public override string Description
{
get { return base.decoratedBeverage.Description + ", soy"; }
}

public override decimal Cost
{
get { return base.decoratedBeverage.Cost + cost; }
}
}
}
20 changes: 20 additions & 0 deletions src/StarbuzzCoffee/StarbuzzCoffee/Condiments/Whip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

namespace StarbuzzCoffee.Condiments
{
class Whip : CondimentDecorator
{
private readonly decimal cost = 0.89M;

public Whip(Beverage beverage) : base(beverage) { }

public override string Description
{
get { return base.decoratedBeverage.Description + ", whip"; }
}

public override decimal Cost
{
get { return base.decoratedBeverage.Cost + cost; }
}
}
}
2 changes: 1 addition & 1 deletion src/StarbuzzCoffee/StarbuzzCoffee/DarkRoast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override string Description

public override decimal Cost
{
get { return 3.99M + CondimentCost; }
get { return 3.99M; }
}
}
}
2 changes: 1 addition & 1 deletion src/StarbuzzCoffee/StarbuzzCoffee/Decaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override string Description

public override decimal Cost
{
get { return 2.49M + CondimentCost; }
get { return 2.49M; }
}
}
}
2 changes: 1 addition & 1 deletion src/StarbuzzCoffee/StarbuzzCoffee/Espresso.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override string Description

public override decimal Cost
{
get { return 2.79M + CondimentCost; }
get { return 2.79M; }
}
}
}
2 changes: 1 addition & 1 deletion src/StarbuzzCoffee/StarbuzzCoffee/HouseBlend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override string Description

public override decimal Cost
{
get { return 2.99M + CondimentCost; }
get { return 2.99M; }
}
}
}
31 changes: 28 additions & 3 deletions src/StarbuzzCoffee/StarbuzzCoffee/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StarbuzzCoffee.Condiments;

namespace StarbuzzCoffee
{
class Program
{
static void Main(string[] args)
{
PrintEspresso();
PrintDarkRoastDoubleMochaWhip();
PrintHouseBlendSoyMochaWhip();
}

private static void PrintHouseBlendSoyMochaWhip()
{
Beverage beverage = new HouseBlend();
beverage = new Soy(beverage);
beverage = new Mocha(beverage);
beverage = new Whip(beverage);
Console.Out.WriteLine(beverage);
}

private static void PrintDarkRoastDoubleMochaWhip()
{
Beverage beverage = new DarkRoast();
beverage = new Mocha(beverage);
beverage = new Mocha(beverage);
beverage = new Whip(beverage);
Console.Out.WriteLine(beverage);
}

private static void PrintEspresso()
{
Beverage beverage = new Espresso();
Console.Out.WriteLine(beverage);
}
}
}
6 changes: 6 additions & 0 deletions src/StarbuzzCoffee/StarbuzzCoffee/StarbuzzCoffee.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Beverage.cs" />
<Compile Include="Condiments\CondimentDecorator.cs" />
<Compile Include="Condiments\Milk.cs" />
<Compile Include="Condiments\Mocha.cs" />
<Compile Include="Condiments\Soy.cs" />
<Compile Include="Condiments\Whip.cs" />
<Compile Include="Espresso.cs" />
<Compile Include="Decaf.cs" />
<Compile Include="DarkRoast.cs" />
<Compile Include="HouseBlend.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down

0 comments on commit e1be047

Please sign in to comment.