Skip to content

Commit 866b36a

Browse files
committed
Initial commit
0 parents  commit 866b36a

12 files changed

+718
-0
lines changed

.gitignore

+442
Large diffs are not rendered by default.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"# DesignPatternsLibrary"
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

src/BuildingBlocks/PatternExecutor.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace DesignPatternsLibrary.PatternExecutors
2+
{
3+
public abstract class PatternExecutor
4+
{
5+
public PatternExecutor()
6+
{
7+
Name = GetType().Name;
8+
}
9+
10+
public string Name { get; init; }
11+
12+
public abstract void Execute();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Greeter
4+
{
5+
public abstract class BaseGreeter
6+
{
7+
public virtual void Greet()
8+
{
9+
Console.WriteLine($"Greetings from {GetType().Name}!");
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\..\BuildingBlocks\BuildingBlocks.csproj" />
9+
</ItemGroup>
10+
11+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Greeter;
2+
using System;
3+
4+
namespace SingletonLibrary.Implementations
5+
{
6+
public class SingletonGreeter : BaseGreeter
7+
{
8+
private static SingletonGreeter instance = null;
9+
10+
private SingletonGreeter()
11+
{
12+
Console.WriteLine("Simple singleton instantiated for the first and only time!");
13+
}
14+
15+
public static SingletonGreeter Instance
16+
{
17+
get
18+
{
19+
Console.WriteLine("Simple singleton being requested...");
20+
21+
if (instance == null)
22+
{
23+
instance = new SingletonGreeter();
24+
}
25+
26+
Console.WriteLine("Simple singleton returned");
27+
return instance;
28+
}
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using DesignPatternsLibrary.PatternExecutors;
2+
using SingletonLibrary.Implementations;
3+
using System;
4+
5+
namespace SingletonLibrary
6+
{
7+
public class SingletonGreeterExecutor : PatternExecutor
8+
{
9+
public override void Execute()
10+
{
11+
Console.WriteLine("--------------------------");
12+
Console.WriteLine("Initial greetings...");
13+
Console.WriteLine();
14+
15+
Greet();
16+
17+
Console.WriteLine("--------------------------");
18+
Console.WriteLine("Goodbye greetings...");
19+
Console.WriteLine();
20+
21+
Greet();
22+
}
23+
24+
private void Greet()
25+
{
26+
SingletonGreeter.Instance.Greet();
27+
}
28+
}
29+
}

src/DesignPatternsLibrary.sln

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30711.63
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesignPatternsLibrary", "DesignPatternsLibrary\DesignPatternsLibrary.csproj", "{60C3B7E4-E290-47F4-A3C0-4F9505DE926F}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BehavioralPatterns", "BehavioralPatterns", "{3FE65E51-30B0-47A3-B4E7-F89721D5F8A2}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CreationalPatterns", "CreationalPatterns", "{C37279A6-C320-46CF-9DAD-BA14DCB91705}"
11+
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "StructuralPatterns", "StructuralPatterns", "{1781BBBF-5241-451D-8170-93B0C3EFD8D5}"
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "OtherPatterns", "OtherPatterns", "{15B15698-CB27-4F30-884C-25C2C1D50202}"
15+
EndProject
16+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Singleton", "Singleton", "{AC5E9989-20E0-44B3-A9C8-CD7AC0E7532F}"
17+
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuildingBlocks", "BuildingBlocks\BuildingBlocks.csproj", "{1CA5ED9E-D727-459B-8EB2-C7E9D5941F85}"
19+
EndProject
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Greeter", "CreationalPatterns\Singleton\Greeter\Greeter.csproj", "{B7BD1BB5-F249-4FB4-B2EF-554DC08659BE}"
21+
EndProject
22+
Global
23+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
24+
Debug|Any CPU = Debug|Any CPU
25+
Release|Any CPU = Release|Any CPU
26+
EndGlobalSection
27+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
28+
{60C3B7E4-E290-47F4-A3C0-4F9505DE926F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{60C3B7E4-E290-47F4-A3C0-4F9505DE926F}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{60C3B7E4-E290-47F4-A3C0-4F9505DE926F}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{60C3B7E4-E290-47F4-A3C0-4F9505DE926F}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{1CA5ED9E-D727-459B-8EB2-C7E9D5941F85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{1CA5ED9E-D727-459B-8EB2-C7E9D5941F85}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{1CA5ED9E-D727-459B-8EB2-C7E9D5941F85}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{1CA5ED9E-D727-459B-8EB2-C7E9D5941F85}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{B7BD1BB5-F249-4FB4-B2EF-554DC08659BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{B7BD1BB5-F249-4FB4-B2EF-554DC08659BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{B7BD1BB5-F249-4FB4-B2EF-554DC08659BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{B7BD1BB5-F249-4FB4-B2EF-554DC08659BE}.Release|Any CPU.Build.0 = Release|Any CPU
40+
EndGlobalSection
41+
GlobalSection(SolutionProperties) = preSolution
42+
HideSolutionNode = FALSE
43+
EndGlobalSection
44+
GlobalSection(NestedProjects) = preSolution
45+
{AC5E9989-20E0-44B3-A9C8-CD7AC0E7532F} = {C37279A6-C320-46CF-9DAD-BA14DCB91705}
46+
{B7BD1BB5-F249-4FB4-B2EF-554DC08659BE} = {AC5E9989-20E0-44B3-A9C8-CD7AC0E7532F}
47+
EndGlobalSection
48+
GlobalSection(ExtensibilityGlobals) = postSolution
49+
SolutionGuid = {F9238CD9-9068-4B63-8D9E-53684BA60A21}
50+
EndGlobalSection
51+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\CreationalPatterns\Singleton\Greeter\Greeter.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using DesignPatternsLibrary.PatternExecutors;
2+
using SingletonLibrary;
3+
using System.Collections.Generic;
4+
5+
namespace DesignPatternsLibrary
6+
{
7+
public class PatternExecutorsRegistry
8+
{
9+
private static PatternExecutorsRegistry _instance = null;
10+
11+
private readonly SortedDictionary<int, PatternExecutor> _executors;
12+
13+
private PatternExecutorsRegistry()
14+
{
15+
_executors = new SortedDictionary<int, PatternExecutor>
16+
{
17+
{ 1, new SingletonGreeterExecutor() }
18+
};
19+
}
20+
21+
public static PatternExecutorsRegistry Instance
22+
{
23+
get
24+
{
25+
if (_instance == null)
26+
{
27+
_instance = new PatternExecutorsRegistry();
28+
}
29+
30+
return _instance;
31+
}
32+
}
33+
34+
public SortedDictionary<int, PatternExecutor> GetAll()
35+
{
36+
return _executors;
37+
}
38+
}
39+
}

src/DesignPatternsLibrary/Program.cs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using DesignPatternsLibrary.PatternExecutors;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace DesignPatternsLibrary
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
string choice;
12+
var executors = PatternExecutorsRegistry.Instance.GetAll();
13+
14+
while (!(choice = SelectFromMenu(executors)).Equals("q"))
15+
{
16+
if (!int.TryParse(choice, out int translatedChoice))
17+
{
18+
Console.WriteLine("Please choose one of the following!");
19+
continue;
20+
}
21+
22+
if (!executors.ContainsKey(translatedChoice))
23+
{
24+
Console.WriteLine("Please choose one of the following!");
25+
continue;
26+
}
27+
28+
executors[translatedChoice].Execute();
29+
30+
}
31+
32+
Console.WriteLine("Thank you!");
33+
Console.ReadLine();
34+
}
35+
36+
static string SelectFromMenu(SortedDictionary<int, PatternExecutor> executors)
37+
{
38+
ListAvailableMenuOptions(executors);
39+
var choice = ChooseOneOption();
40+
41+
Console.ResetColor();
42+
43+
return choice;
44+
}
45+
46+
static void ListAvailableMenuOptions(SortedDictionary<int, PatternExecutor> executors)
47+
{
48+
Console.ForegroundColor = ConsoleColor.DarkGreen;
49+
Console.WriteLine(Environment.NewLine);
50+
51+
foreach (var executor in executors)
52+
{
53+
Console.WriteLine($"{executor.Key}. {executor.Value.Name}");
54+
}
55+
56+
Console.WriteLine("q. Quit");
57+
}
58+
59+
static string ChooseOneOption()
60+
{
61+
Console.ForegroundColor = ConsoleColor.DarkGray;
62+
Console.WriteLine($"\n Your choice: ");
63+
64+
return Console.ReadLine().ToLower();
65+
}
66+
67+
68+
}
69+
}

0 commit comments

Comments
 (0)