Skip to content

Commit a4bf68b

Browse files
committed
Decorator Pattern Changes
1 parent 4a217bb commit a4bf68b

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

DecoratorPattern/Program.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
3+
namespace DecoratorPattern
4+
{
5+
interface ICoffee
6+
{
7+
int GetCost();
8+
string GetDescription();
9+
}
10+
11+
class SimpleCoffee : ICoffee
12+
{
13+
public int GetCost()
14+
{
15+
return 5;
16+
}
17+
18+
public string GetDescription()
19+
{
20+
return "Simple Coffee";
21+
}
22+
}
23+
24+
class MilkCoffee : ICoffee
25+
{
26+
private readonly ICoffee mCoffee;
27+
28+
public MilkCoffee(ICoffee coffee)
29+
{
30+
mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null");
31+
}
32+
public int GetCost()
33+
{
34+
return mCoffee.GetCost() + 1;
35+
}
36+
37+
public string GetDescription()
38+
{
39+
return String.Concat(mCoffee.GetDescription(), ", milk");
40+
}
41+
}
42+
43+
class WhipCoffee : ICoffee
44+
{
45+
private readonly ICoffee mCoffee;
46+
47+
public WhipCoffee(ICoffee coffee)
48+
{
49+
mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null");
50+
}
51+
public int GetCost()
52+
{
53+
return mCoffee.GetCost() + 1;
54+
}
55+
56+
public string GetDescription()
57+
{
58+
return String.Concat(mCoffee.GetDescription(), ", whip");
59+
}
60+
}
61+
62+
class VanillaCoffee : ICoffee
63+
{
64+
private readonly ICoffee mCoffee;
65+
66+
public VanillaCoffee(ICoffee coffee)
67+
{
68+
mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null");
69+
}
70+
public int GetCost()
71+
{
72+
return mCoffee.GetCost() + 1;
73+
}
74+
75+
public string GetDescription()
76+
{
77+
return String.Concat(mCoffee.GetDescription(), ", vanilla");
78+
}
79+
}
80+
class Program
81+
{
82+
static void Main(string[] args)
83+
{
84+
var myCoffee = new SimpleCoffee();
85+
Console.WriteLine("{0:c}",myCoffee.GetCost()); // $ 5.00
86+
Console.WriteLine("{0}", myCoffee.GetDescription()); // Simple Coffee
87+
88+
var milkCoffee = new MilkCoffee(myCoffee);
89+
Console.WriteLine("{0:c}", milkCoffee.GetCost()); // $ 6.00
90+
Console.WriteLine("{0}", milkCoffee.GetDescription()); // Simple Coffee, milk
91+
92+
var whipCoffee = new WhipCoffee(milkCoffee);
93+
Console.WriteLine("{0:c}", whipCoffee.GetCost()); // $ 7.00
94+
Console.WriteLine("{0}", whipCoffee.GetDescription()); // Simple Coffee, milk, whip
95+
96+
var vanillaCoffee = new VanillaCoffee(whipCoffee);
97+
Console.WriteLine("{0:c}", vanillaCoffee.GetCost()); // $ 8.00
98+
Console.WriteLine("{0}", vanillaCoffee.GetDescription()); // Simple Coffee, milk, whip
99+
Console.ReadLine();
100+
}
101+
}
102+
}

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompositePattern", "Composi
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FacadePattern", "FacadePattern\FacadePattern.csproj", "{8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DecoratorPattern", "DecoratorPattern\DecoratorPattern.csproj", "{E16867D5-A235-437D-ADBF-8A4418E7038B}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -63,6 +65,10 @@ Global
6365
{8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}.Debug|Any CPU.Build.0 = Debug|Any CPU
6466
{8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}.Release|Any CPU.ActiveCfg = Release|Any CPU
6567
{8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{E16867D5-A235-437D-ADBF-8A4418E7038B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{E16867D5-A235-437D-ADBF-8A4418E7038B}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{E16867D5-A235-437D-ADBF-8A4418E7038B}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{E16867D5-A235-437D-ADBF-8A4418E7038B}.Release|Any CPU.Build.0 = Release|Any CPU
6672
EndGlobalSection
6773
GlobalSection(SolutionProperties) = preSolution
6874
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)