main
syneffort 2 years ago
parent 96c37dc68a
commit 8ff0f9df49
  1. 15
      DesignPatternGuru/DesignPatternGuru.sln
  2. 23
      DesignPatternGuru/Singleton/BasicSingleton/Client.cs
  3. 26
      DesignPatternGuru/Singleton/BasicSingleton/Singleton.cs
  4. 14
      DesignPatternGuru/Singleton/Program.cs
  5. 10
      DesignPatternGuru/Singleton/Singleton.csproj
  6. 41
      DesignPatternGuru/Singleton/ThreadSageSingleton/Client.cs
  7. 39
      DesignPatternGuru/Singleton/ThreadSageSingleton/Singleton.cs

@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Builder\Builder.
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Prototype\Prototype.csproj", "{3A8E007E-79D4-499B-8D19-BF745EC9C68E}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Prototype\Prototype.csproj", "{3A8E007E-79D4-499B-8D19-BF745EC9C68E}"
EndProject 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{3A8E007E-79D4-499B-8D19-BF745EC9C68E}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection 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 GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC} SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}
EndGlobalSection EndGlobalSection

@ -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");
}
}
}

@ -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;
}
}
}

@ -0,0 +1,14 @@
namespace Singleton
{
internal class Program
{
static void Main(string[] args)
{
BasicSingleton.Client.DoTest();
Console.WriteLine();
ThreadSageSingleton.Client.DoTest();
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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);
}
}
}

@ -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;
}
}
}
Loading…
Cancel
Save