diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index 5a81c07..359e2ad 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Builder\Builder. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Prototype\Prototype.csproj", "{3A8E007E-79D4-499B-8D19-BF745EC9C68E}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CreationalPatterns", "CreationalPatterns", "{C470237E-3C3B-49E3-BA4C-86884EC28413}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Singleton", "Singleton\Singleton.csproj", "{79AC3F3A-CE95-40C4-94E6-31CC85494E41}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,10 +37,21 @@ Global {3A8E007E-79D4-499B-8D19-BF745EC9C68E}.Debug|Any CPU.Build.0 = Debug|Any CPU {3A8E007E-79D4-499B-8D19-BF745EC9C68E}.Release|Any CPU.ActiveCfg = Release|Any CPU {3A8E007E-79D4-499B-8D19-BF745EC9C68E}.Release|Any CPU.Build.0 = Release|Any CPU + {79AC3F3A-CE95-40C4-94E6-31CC85494E41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79AC3F3A-CE95-40C4-94E6-31CC85494E41}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79AC3F3A-CE95-40C4-94E6-31CC85494E41}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79AC3F3A-CE95-40C4-94E6-31CC85494E41}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {4F58C486-7121-435B-B7B2-F3ADD0FBE2AE} = {C470237E-3C3B-49E3-BA4C-86884EC28413} + {8310590F-E10F-4845-8737-AF3CEF4583B1} = {C470237E-3C3B-49E3-BA4C-86884EC28413} + {FD2896D5-8199-4036-8D45-1CC0D3B4B79F} = {C470237E-3C3B-49E3-BA4C-86884EC28413} + {3A8E007E-79D4-499B-8D19-BF745EC9C68E} = {C470237E-3C3B-49E3-BA4C-86884EC28413} + {79AC3F3A-CE95-40C4-94E6-31CC85494E41} = {C470237E-3C3B-49E3-BA4C-86884EC28413} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC} EndGlobalSection diff --git a/DesignPatternGuru/Singleton/BasicSingleton/Client.cs b/DesignPatternGuru/Singleton/BasicSingleton/Client.cs new file mode 100644 index 0000000..bbd3e28 --- /dev/null +++ b/DesignPatternGuru/Singleton/BasicSingleton/Client.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Singleton.BasicSingleton +{ + internal class Client + { + public static void DoTest() + { + Console.WriteLine("Basic Singleton"); + Singleton s1 = Singleton.GetInstance(); + Singleton s2 = Singleton.GetInstance(); + + if (s1 == s2) + Console.WriteLine("Both instances are same"); + else + Console.WriteLine("Instances are different"); + } + } +} diff --git a/DesignPatternGuru/Singleton/BasicSingleton/Singleton.cs b/DesignPatternGuru/Singleton/BasicSingleton/Singleton.cs new file mode 100644 index 0000000..b047900 --- /dev/null +++ b/DesignPatternGuru/Singleton/BasicSingleton/Singleton.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Singleton.BasicSingleton +{ + internal class Singleton + { + private static Singleton _instance; + + private Singleton() + { + + } + + public static Singleton GetInstance() + { + if (_instance == null) + _instance = new Singleton(); + + return _instance; + } + } +} diff --git a/DesignPatternGuru/Singleton/Program.cs b/DesignPatternGuru/Singleton/Program.cs new file mode 100644 index 0000000..a7dd79f --- /dev/null +++ b/DesignPatternGuru/Singleton/Program.cs @@ -0,0 +1,14 @@ +namespace Singleton +{ + internal class Program + { + static void Main(string[] args) + { + BasicSingleton.Client.DoTest(); + + Console.WriteLine(); + + ThreadSageSingleton.Client.DoTest(); + } + } +} \ No newline at end of file diff --git a/DesignPatternGuru/Singleton/Singleton.csproj b/DesignPatternGuru/Singleton/Singleton.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/DesignPatternGuru/Singleton/Singleton.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DesignPatternGuru/Singleton/ThreadSageSingleton/Client.cs b/DesignPatternGuru/Singleton/ThreadSageSingleton/Client.cs new file mode 100644 index 0000000..5148bd5 --- /dev/null +++ b/DesignPatternGuru/Singleton/ThreadSageSingleton/Client.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Singleton.ThreadSageSingleton +{ + internal class Client + { + public static void DoTest() + { + Console.WriteLine( + "Thread safe Singleton", + "If you see the same value, then both instances are same.", + "If you see differnt values, then 2 differnect instances are created.", + "RESULT: "); + + Thread process1 = new Thread(() => + { + TestSingleton("FOO"); + }); + Thread process2 = new Thread(() => + { + TestSingleton("BAR"); + }); + + process1.Start(); + process2.Start(); + + process1.Join(); + process2.Join(); + } + + public static void TestSingleton(string value) + { + Singleton singleton = Singleton.GetInstance(value); + Console.WriteLine(singleton.Value); + } + } +} diff --git a/DesignPatternGuru/Singleton/ThreadSageSingleton/Singleton.cs b/DesignPatternGuru/Singleton/ThreadSageSingleton/Singleton.cs new file mode 100644 index 0000000..1b24861 --- /dev/null +++ b/DesignPatternGuru/Singleton/ThreadSageSingleton/Singleton.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Singleton.ThreadSageSingleton +{ + internal class Singleton + { + private static Singleton _instance; + + private static readonly object _lock = new object(); + + public string Value { get; set; } + + private Singleton() + { + + } + + public static Singleton GetInstance(string value) + { + if (_instance == null) + { + lock (_lock) + { + if (_instance == null) + { + _instance = new Singleton(); + _instance.Value = value; + } + } + } + + return _instance; + } + } +}