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 7973a53
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
<ProjectReference Include="..\DesignPatterns.Visitor\DesignPatterns.Visitor.csproj" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>DesignPatterns.Visitor</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>DesignPatterns.AbstractFactory.UnitTests</RootNamespace>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
Expand Down Expand Up @@ -34,4 +33,4 @@
<ProjectReference Include="..\DesignPatterns.AbstractFactory\DesignPatterns.AbstractFactory.csproj" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>DesignPatterns.AbstractFactory</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand All @@ -19,4 +18,4 @@
</None>
</ItemGroup>

</Project>
</Project>
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>
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);
}
}
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);
}
}
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>
9 changes: 9 additions & 0 deletions Creational/DesignPatterns.Singleton/Program.cs
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();
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 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();
}
}
}
}
14 changes: 14 additions & 0 deletions DesignPatterns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ 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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Singleton.UnitTests", "Creational\DesignPatterns.Singleton.UnitTests\DesignPatterns.Singleton.UnitTests.csproj", "{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -42,12 +46,22 @@ 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
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}.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}
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
EndGlobalSection
EndGlobal

0 comments on commit 7973a53

Please sign in to comment.