Skip to content

Commit 67c3a00

Browse files
Replaced Composite pattern with .NET Core project.
1 parent 84defaf commit 67c3a00

File tree

4 files changed

+145
-6
lines changed

4 files changed

+145
-6
lines changed

CompositeCore/Composite.csproj

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

CompositeCore/Drinks.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Composite
8+
{
9+
/// <summary>
10+
/// Component abstract class
11+
/// </summary>
12+
public abstract class SoftDrink
13+
{
14+
public int Calories { get; set; }
15+
16+
public List<SoftDrink> Flavors { get; set; }
17+
18+
public SoftDrink(int calories)
19+
{
20+
Calories = calories;
21+
Flavors = new List<SoftDrink>();
22+
}
23+
24+
/// <summary>
25+
/// "Flatten" method, returns all available flavors
26+
/// </summary>
27+
public void DisplayCalories()
28+
{
29+
Console.WriteLine(this.GetType().Name + ": " + this.Calories.ToString() + " calories.");
30+
foreach (var drink in this.Flavors)
31+
{
32+
drink.DisplayCalories();
33+
}
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Leaf class
39+
/// </summary>
40+
public class VanillaCola : SoftDrink
41+
{
42+
public VanillaCola(int calories) : base(calories) { }
43+
}
44+
45+
/// <summary>
46+
/// Leaf class
47+
/// </summary>
48+
public class CherryCola : SoftDrink
49+
{
50+
public CherryCola(int calories) : base(calories) { }
51+
}
52+
53+
/// <summary>
54+
/// Leaf class
55+
/// </summary>
56+
public class StrawberryRootBeer : SoftDrink
57+
{
58+
public StrawberryRootBeer(int calories) : base(calories) { }
59+
}
60+
61+
/// <summary>
62+
/// Leaf class
63+
/// </summary>
64+
public class VanillaRootBeer : SoftDrink
65+
{
66+
public VanillaRootBeer(int calories) : base(calories) { }
67+
}
68+
69+
/// <summary>
70+
/// Composite class
71+
/// </summary>
72+
public class Cola : SoftDrink
73+
{
74+
public Cola(int calories) : base(calories) { }
75+
}
76+
77+
/// <summary>
78+
/// Leaf class
79+
/// </summary>
80+
public class LemonLime : SoftDrink
81+
{
82+
public LemonLime(int calories) : base(calories) { }
83+
}
84+
85+
/// <summary>
86+
/// Composite class
87+
/// </summary>
88+
public class RootBeer : SoftDrink
89+
{
90+
public RootBeer(int calories) : base(calories) { }
91+
}
92+
93+
/// <summary>
94+
/// Composite class, root node
95+
/// </summary>
96+
public class SodaWater : SoftDrink
97+
{
98+
public SodaWater(int calories) : base(calories) { }
99+
}
100+
}

CompositeCore/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace Composite
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
var colas = new Cola(210);
10+
colas.Flavors.Add(new VanillaCola(215));
11+
colas.Flavors.Add(new CherryCola(210));
12+
13+
var lemonLime = new LemonLime(185);
14+
15+
var rootBeers = new RootBeer(195);
16+
rootBeers.Flavors.Add(new VanillaRootBeer(200));
17+
rootBeers.Flavors.Add(new StrawberryRootBeer(200));
18+
19+
SodaWater sodaWater = new SodaWater(180);
20+
sodaWater.Flavors.Add(colas);
21+
sodaWater.Flavors.Add(lemonLime);
22+
sodaWater.Flavors.Add(rootBeers);
23+
24+
sodaWater.DisplayCalories();
25+
26+
Console.ReadKey();
27+
}
28+
}
29+
}

DesignPatterns.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decorator", "Decorator\Deco
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flyweight", "Flyweight\Flyweight.csproj", "{9719E34D-9233-47A0-B655-399DA51A2042}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Composite\Composite.csproj", "{3BCA8A82-0242-434C-9299-C6CD5D49F301}"
21-
EndProject
2220
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iterator", "Iterator\Iterator.csproj", "{0B9618CC-8F2A-48BE-B3D7-352537CC53D9}"
2321
EndProject
2422
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memento", "Memento\Memento.csproj", "{77DCDAD8-B41F-426A-9BFD-6759E550F980}"
@@ -47,6 +45,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "Ch
4745
EndProject
4846
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Command", "CommandCore\Command.csproj", "{6462E20B-F66F-441E-AD5F-5E579D73DAAE}"
4947
EndProject
48+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "CompositeCore\Composite.csproj", "{12E9D67B-E4D8-4A0D-9E20-0A11D5AD9393}"
49+
EndProject
5050
Global
5151
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5252
Debug|Any CPU = Debug|Any CPU
@@ -81,10 +81,6 @@ Global
8181
{9719E34D-9233-47A0-B655-399DA51A2042}.Debug|Any CPU.Build.0 = Debug|Any CPU
8282
{9719E34D-9233-47A0-B655-399DA51A2042}.Release|Any CPU.ActiveCfg = Release|Any CPU
8383
{9719E34D-9233-47A0-B655-399DA51A2042}.Release|Any CPU.Build.0 = Release|Any CPU
84-
{3BCA8A82-0242-434C-9299-C6CD5D49F301}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
85-
{3BCA8A82-0242-434C-9299-C6CD5D49F301}.Debug|Any CPU.Build.0 = Debug|Any CPU
86-
{3BCA8A82-0242-434C-9299-C6CD5D49F301}.Release|Any CPU.ActiveCfg = Release|Any CPU
87-
{3BCA8A82-0242-434C-9299-C6CD5D49F301}.Release|Any CPU.Build.0 = Release|Any CPU
8884
{0B9618CC-8F2A-48BE-B3D7-352537CC53D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
8985
{0B9618CC-8F2A-48BE-B3D7-352537CC53D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
9086
{0B9618CC-8F2A-48BE-B3D7-352537CC53D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -141,6 +137,10 @@ Global
141137
{6462E20B-F66F-441E-AD5F-5E579D73DAAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
142138
{6462E20B-F66F-441E-AD5F-5E579D73DAAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
143139
{6462E20B-F66F-441E-AD5F-5E579D73DAAE}.Release|Any CPU.Build.0 = Release|Any CPU
140+
{12E9D67B-E4D8-4A0D-9E20-0A11D5AD9393}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
141+
{12E9D67B-E4D8-4A0D-9E20-0A11D5AD9393}.Debug|Any CPU.Build.0 = Debug|Any CPU
142+
{12E9D67B-E4D8-4A0D-9E20-0A11D5AD9393}.Release|Any CPU.ActiveCfg = Release|Any CPU
143+
{12E9D67B-E4D8-4A0D-9E20-0A11D5AD9393}.Release|Any CPU.Build.0 = Release|Any CPU
144144
EndGlobalSection
145145
GlobalSection(SolutionProperties) = preSolution
146146
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)