-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1338ca3
commit f359eae
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
Creational/DesignPatterns.Singleton/DesignPatterns.Singleton.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters