-
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 7973a53
Showing
12 changed files
with
157 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
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
40 changes: 40 additions & 0 deletions
40
Creational/DesignPatterns.Singleton.UnitTests/DesignPatterns.Singleton.UnitTests.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,40 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoFixture" Version="4.18.1" /> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="Shouldly" Version="4.2.1" /> | ||
<PackageReference Include="xunit" Version="2.6.5" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.msbuild" Version="6.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DesignPatterns.Singleton\DesignPatterns.Singleton.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DesignPatterns.Singleton\DesignPatterns.Singleton.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
16 changes: 16 additions & 0 deletions
16
Creational/DesignPatterns.Singleton.UnitTests/SingletonWithLazyTests.cs
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 @@ | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace DesignPatterns.Singleton.UnitTests; | ||
|
||
public class SingletonWithLazyTests | ||
{ | ||
[Fact] | ||
public void Given_WhenCreateSingletonWithLazy_ThenSameInstancesReturned() | ||
{ | ||
var firstInstance = SingletonWithLazy.Instance; | ||
var secondInstance = SingletonWithLazy.Instance; | ||
|
||
firstInstance.ShouldBe(secondInstance); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Creational/DesignPatterns.Singleton.UnitTests/SingletonWithLockTests.cs
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 @@ | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace DesignPatterns.Singleton.UnitTests; | ||
|
||
public class SingletonWithLockTests | ||
{ | ||
[Fact] | ||
public void Given_WhenCreateSingletonWithLock_ThenSameInstancesReturned() | ||
{ | ||
var firstInstance = SingletonWithLock.Instance; | ||
var secondInstance = SingletonWithLock.Instance; | ||
|
||
firstInstance.ShouldBe(secondInstance); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</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,9 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
var host = Host.CreateDefaultBuilder(args) | ||
.ConfigureAppConfiguration(ctx => ctx.AddJsonFile("appsettings.json")) | ||
.ConfigureServices(services => { }) | ||
.Build(); | ||
|
||
Console.WriteLine(); |
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 lockObj = 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 (lockObj) | ||
{ | ||
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