Skip to content

Commit 21ebe47

Browse files
author
Bogdan Hladiuc
committed
Design patterns: Factory, Builder, Singleton
1 parent a6afaa9 commit 21ebe47

File tree

12 files changed

+554
-3
lines changed

12 files changed

+554
-3
lines changed

Design Patterns/Builder/Builder.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#nullable disable warnings
2+
3+
namespace DesignPatterns.Builder;
4+
5+
public class Builder {
6+
internal class Burger {
7+
string Bun { get; set; }
8+
string Patty { get; set; }
9+
string Cheese { get; set; }
10+
11+
public void SetBuns(string bun) {
12+
Bun = bun;
13+
}
14+
15+
public void SetPatty(string patty) {
16+
Patty = patty;
17+
}
18+
19+
public void SetCheese(string cheese) {
20+
Cheese = cheese;
21+
}
22+
23+
public void Print() {
24+
Console.WriteLine($"[{Bun}, {Patty}, {Cheese}]");
25+
}
26+
}
27+
28+
internal class BurgerBuilder {
29+
Burger Burger { get; set; }
30+
31+
public BurgerBuilder() {
32+
Burger = new Burger();
33+
}
34+
35+
public BurgerBuilder AddBuns(string bunStyle) {
36+
Burger.SetBuns(bunStyle);
37+
return this;
38+
}
39+
40+
public BurgerBuilder AddPatty(string pattyStyle) {
41+
Burger.SetPatty(pattyStyle);
42+
return this;
43+
}
44+
45+
public BurgerBuilder AddCheese(string cheeseStyle) {
46+
Burger.SetCheese(cheeseStyle);
47+
return this;
48+
}
49+
50+
public Burger Build() {
51+
return Burger;
52+
}
53+
}
54+
55+
public static void Run() {
56+
var burger = new BurgerBuilder()
57+
.AddBuns("sesame")
58+
.AddPatty("beef-patty")
59+
.AddCheese("cedar")
60+
.Build();
61+
62+
burger.Print();
63+
}
64+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

Design Patterns/Factory/Factory.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#nullable disable warnings
2+
3+
using Library;
4+
5+
namespace DesignPatterns.Factory;
6+
7+
public class Factory {
8+
internal class Burger {
9+
string[] Ingredients { get; set; }
10+
11+
public Burger(string[] ingredients) {
12+
Ingredients = ingredients;
13+
}
14+
15+
public void Print() {
16+
AssortedMethods.PrintStringArray(Ingredients);
17+
}
18+
}
19+
20+
internal class BurgerFactory {
21+
22+
public Burger CreateCheeseBurger() {
23+
var ingredients = new string[] { "bun", "cheese", "beef-patty" };
24+
return new Burger(ingredients);
25+
}
26+
27+
public Burger CreateDeluxeCheeseBurger() {
28+
var ingredients = new string[] { "bun", "tomatoes", "lettuce", "cheese", "beef-patty" };
29+
return new Burger(ingredients);
30+
}
31+
32+
public Burger CreateVeganBurger() {
33+
var ingredients = new string[] { "bun", "special-sauce", "veggie-patty" };
34+
return new Burger(ingredients);
35+
}
36+
}
37+
38+
public static void Run() {
39+
var burgerFactory = new BurgerFactory();
40+
burgerFactory.CreateCheeseBurger().Print();
41+
burgerFactory.CreateDeluxeCheeseBurger().Print();
42+
burgerFactory.CreateVeganBurger().Print();
43+
}
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\Library\Library.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<TargetFramework>net6.0</TargetFramework>
9+
<ImplicitUsings>enable</ImplicitUsings>
10+
<Nullable>enable</Nullable>
11+
</PropertyGroup>
12+
13+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#nullable disable warnings
2+
namespace DesignPatterns.Singleton;
3+
public class Singleton {
4+
5+
sealed class SingletonInstance {
6+
private static readonly SingletonInstance instance = new SingletonInstance();
7+
public bool IsLoggedIn { get; set; }
8+
9+
private SingletonInstance() {
10+
IsLoggedIn = false;
11+
}
12+
13+
public static SingletonInstance Instance {
14+
get {
15+
return instance;
16+
}
17+
}
18+
}
19+
20+
public static void Run() {
21+
var singleton1 = SingletonInstance.Instance;
22+
Console.WriteLine($"singleton1.IsLoggedIn = {singleton1.IsLoggedIn}");
23+
24+
var singleton2 = SingletonInstance.Instance;
25+
singleton2.IsLoggedIn = true;
26+
Console.WriteLine($"singleton1.IsLoggedIn = {singleton1.IsLoggedIn}");
27+
Console.WriteLine($"singleton2.IsLoggedIn = {singleton2.IsLoggedIn}");
28+
}
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

LeetcodeNew.sln

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_21_Positions_Are_Safe",
129129
EndProject
130130
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q91_Decode_Ways", "Q91_Decode_Ways\Q91_Decode_Ways.csproj", "{58453981-B2A6-49F0-9E56-AAFB5532C244}"
131131
EndProject
132+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{0F05CDCB-91D8-45D5-B09E-1FE20A5D322E}"
133+
EndProject
134+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find_Any_One_Duplicate", "Misc\Find_Any_One_Duplicate\Find_Any_One_Duplicate.csproj", "{9190FE79-8571-492D-9EA1-D6D998521DB9}"
135+
EndProject
136+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Design Patterns", "Design Patterns", "{A059A18B-8F12-4AFA-A873-160FCE87B339}"
137+
EndProject
138+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Factory", "Design Patterns\Factory\Factory.csproj", "{6329D6FC-7BCD-477C-8F69-8C84697EC35D}"
139+
EndProject
140+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Design Patterns\Builder\Builder.csproj", "{04B9930E-022A-4425-BFC8-5E4434E94024}"
141+
EndProject
142+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Singleton", "Design Patterns\Singleton\Singleton.csproj", "{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}"
143+
EndProject
132144
Global
133145
GlobalSection(SolutionConfigurationPlatforms) = preSolution
134146
Debug|Any CPU = Debug|Any CPU
@@ -874,6 +886,54 @@ Global
874886
{58453981-B2A6-49F0-9E56-AAFB5532C244}.Release|x64.Build.0 = Release|Any CPU
875887
{58453981-B2A6-49F0-9E56-AAFB5532C244}.Release|x86.ActiveCfg = Release|Any CPU
876888
{58453981-B2A6-49F0-9E56-AAFB5532C244}.Release|x86.Build.0 = Release|Any CPU
889+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
890+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
891+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Debug|x64.ActiveCfg = Debug|Any CPU
892+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Debug|x64.Build.0 = Debug|Any CPU
893+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Debug|x86.ActiveCfg = Debug|Any CPU
894+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Debug|x86.Build.0 = Debug|Any CPU
895+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
896+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Release|Any CPU.Build.0 = Release|Any CPU
897+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Release|x64.ActiveCfg = Release|Any CPU
898+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Release|x64.Build.0 = Release|Any CPU
899+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Release|x86.ActiveCfg = Release|Any CPU
900+
{9190FE79-8571-492D-9EA1-D6D998521DB9}.Release|x86.Build.0 = Release|Any CPU
901+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
902+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Debug|Any CPU.Build.0 = Debug|Any CPU
903+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Debug|x64.ActiveCfg = Debug|Any CPU
904+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Debug|x64.Build.0 = Debug|Any CPU
905+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Debug|x86.ActiveCfg = Debug|Any CPU
906+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Debug|x86.Build.0 = Debug|Any CPU
907+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Release|Any CPU.ActiveCfg = Release|Any CPU
908+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Release|Any CPU.Build.0 = Release|Any CPU
909+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Release|x64.ActiveCfg = Release|Any CPU
910+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Release|x64.Build.0 = Release|Any CPU
911+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Release|x86.ActiveCfg = Release|Any CPU
912+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D}.Release|x86.Build.0 = Release|Any CPU
913+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
914+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Debug|Any CPU.Build.0 = Debug|Any CPU
915+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Debug|x64.ActiveCfg = Debug|Any CPU
916+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Debug|x64.Build.0 = Debug|Any CPU
917+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Debug|x86.ActiveCfg = Debug|Any CPU
918+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Debug|x86.Build.0 = Debug|Any CPU
919+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Release|Any CPU.ActiveCfg = Release|Any CPU
920+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Release|Any CPU.Build.0 = Release|Any CPU
921+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Release|x64.ActiveCfg = Release|Any CPU
922+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Release|x64.Build.0 = Release|Any CPU
923+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Release|x86.ActiveCfg = Release|Any CPU
924+
{04B9930E-022A-4425-BFC8-5E4434E94024}.Release|x86.Build.0 = Release|Any CPU
925+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
926+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
927+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Debug|x64.ActiveCfg = Debug|Any CPU
928+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Debug|x64.Build.0 = Debug|Any CPU
929+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Debug|x86.ActiveCfg = Debug|Any CPU
930+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Debug|x86.Build.0 = Debug|Any CPU
931+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
932+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Release|Any CPU.Build.0 = Release|Any CPU
933+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Release|x64.ActiveCfg = Release|Any CPU
934+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Release|x64.Build.0 = Release|Any CPU
935+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Release|x86.ActiveCfg = Release|Any CPU
936+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8}.Release|x86.Build.0 = Release|Any CPU
877937
EndGlobalSection
878938
GlobalSection(NestedProjects) = preSolution
879939
{45EB27A3-9FBB-48ED-8C89-E0F074C02E8C} = {71C7D88B-6D5D-4E25-A275-869603700831}
@@ -898,5 +958,9 @@ Global
898958
{A60BC75A-CFB2-4DD0-ABB6-996B1A7A365E} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
899959
{59CEF2C6-5BC8-4F83-ABA7-50C19533AD40} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
900960
{5CD502FA-339D-4637-A1BF-BC5B32DCAD38} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
961+
{9190FE79-8571-492D-9EA1-D6D998521DB9} = {0F05CDCB-91D8-45D5-B09E-1FE20A5D322E}
962+
{6329D6FC-7BCD-477C-8F69-8C84697EC35D} = {A059A18B-8F12-4AFA-A873-160FCE87B339}
963+
{04B9930E-022A-4425-BFC8-5E4434E94024} = {A059A18B-8F12-4AFA-A873-160FCE87B339}
964+
{22DF60F2-E5FE-4A35-89C3-5EB577ABEAD8} = {A059A18B-8F12-4AFA-A873-160FCE87B339}
901965
EndGlobalSection
902966
EndGlobal

Run/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static void Main(string[] args) {
3838
// Q56_Merge_Intervals.Q56_Merge_Intervals.Run();
3939
// Q65_Valid_Number.Q65_Valid_Number.Run();
4040
// Q71_Simplify_Path.Q71_Simplify_Path.Run();
41-
Q91_Decode_Ways.DecodeWays.Run();
41+
// Q91_Decode_Ways.DecodeWays.Run();
4242
// Q152_Maximum_Product_Subarray.Q152_Maximum_Product_Subarray.Run();
4343
// Q173_Binary_Search_Tree_Iterator.Q173_Binary_Search_Tree_Iterator.Run();
4444
// Q189_Rotate_Array.Q189_Rotate_Array.Run();
@@ -70,6 +70,11 @@ static void Main(string[] args) {
7070

7171
// Misc
7272
// Counting_Sort.Counting_Sort.Run();
73+
74+
// Design Patterns
75+
// DesignPatterns.Factory.Factory.Run();
76+
// DesignPatterns.Builder.Builder.Run();
77+
DesignPatterns.Singleton.Singleton.Run();
7378
}
7479
}
7580
}

Run/Run.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_20_Set_Matrix_To_Zero\Day_20_Set_Matrix_To_Zero.csproj" />
6363
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_21_Positions_Are_Safe\Day_21_Positions_Are_Safe.csproj" />
6464
<ProjectReference Include="..\Q91_Decode_Ways\Q91_Decode_Ways.csproj" />
65+
<ProjectReference Include="..\Misc\Find_Any_One_Duplicate\Find_Any_One_Duplicate.csproj" />
66+
<ProjectReference Include="..\Design Patterns\Factory\Factory.csproj" />
67+
<ProjectReference Include="..\Design Patterns\Builder\Builder.csproj" />
68+
<ProjectReference Include="..\Design Patterns\Singleton\Singleton.csproj" />
6569
</ItemGroup>
6670

6771
<PropertyGroup>

0 commit comments

Comments
 (0)