Skip to content

Commit

Permalink
Add Singleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 8, 2024
1 parent 1338ca3 commit f359eae
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
16 changes: 16 additions & 0 deletions Creational/DesignPatterns.Singleton/SingletonWithLazy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace DesignPatterns.Singleton;

public class SingletonWithLazy
{
private static readonly Lazy<SingletonWithLazy> instance = new(() => new SingletonWithLazy());

/// <summary>
/// The private constructor is declared to prevent instantiation directly, via calling a default constructor.
/// </summary>
private SingletonWithLazy()
{
Console.WriteLine($"An instance of {nameof(SingletonWithLazy)} has been created.");
}

public static SingletonWithLazy Instance => instance.Value;
}
28 changes: 28 additions & 0 deletions Creational/DesignPatterns.Singleton/SingletonWithLock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace DesignPatterns.Singleton;

public class SingletonWithLock
{
private static SingletonWithLock? instance = null;
private static readonly object syncObject = new();

/// <summary>
/// The private constructor is declared to prevent instantiation directly, via calling a default constructor.
/// </summary>
private SingletonWithLock()
{
Console.WriteLine($"An instance of {nameof(SingletonWithLock)} has been created.");
}

public static SingletonWithLock Instance
{
get
{
// The cost of executing the lock operation is significantly higher
// in comparison to the straightforward pointer check `instance != null`.
lock (syncObject)
{
return instance ??= new SingletonWithLock();
}
}
}
}
7 changes: 7 additions & 0 deletions DesignPatterns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Structural", "Structural",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Decorator", "Structural\DesignPatterns.Decorator\DesignPatterns.Decorator.csproj", "{FA3C4F09-104E-4F7D-9ADB-E7F22917ADF7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Singleton", "Creational\DesignPatterns.Singleton\DesignPatterns.Singleton.csproj", "{658DB1C6-AC85-4E03-99C4-C79116E558F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -42,12 +44,17 @@ Global
{FA3C4F09-104E-4F7D-9ADB-E7F22917ADF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA3C4F09-104E-4F7D-9ADB-E7F22917ADF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA3C4F09-104E-4F7D-9ADB-E7F22917ADF7}.Release|Any CPU.Build.0 = Release|Any CPU
{658DB1C6-AC85-4E03-99C4-C79116E558F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{658DB1C6-AC85-4E03-99C4-C79116E558F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{658DB1C6-AC85-4E03-99C4-C79116E558F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{658DB1C6-AC85-4E03-99C4-C79116E558F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
{02AA0F74-2EFE-4804-8938-A92542860F74} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
{0C5CFC73-F6E4-4796-A11C-9CA378B80517} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{1E56CC89-45C1-45F7-9EC8-88D648572816} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{FA3C4F09-104E-4F7D-9ADB-E7F22917ADF7} = {5961ADCC-5FA6-4076-A9F4-C1D3207ABE1A}
{658DB1C6-AC85-4E03-99C4-C79116E558F5} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
EndGlobalSection
EndGlobal

0 comments on commit f359eae

Please sign in to comment.