Skip to content

Commit 8c2e591

Browse files
committed
Add Singleton design pattern
1 parent 9298899 commit 8c2e591

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

CSharpDesignPatterns.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Strategy", "Behavioral\Stra
3131
EndProject
3232
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Visitor", "Behavioral\Visitor\Visitor.csproj", "{32E13EA5-C3A3-4DC9-9F2C-B938C7632320}"
3333
EndProject
34+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Singleton", "Creational\Singleton\Singleton.csproj", "{DF0DD259-1E2B-4385-922D-90E782C00106}"
35+
EndProject
3436
Global
3537
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3638
Debug|Any CPU = Debug|Any CPU
@@ -81,6 +83,10 @@ Global
8183
{32E13EA5-C3A3-4DC9-9F2C-B938C7632320}.Debug|Any CPU.Build.0 = Debug|Any CPU
8284
{32E13EA5-C3A3-4DC9-9F2C-B938C7632320}.Release|Any CPU.ActiveCfg = Release|Any CPU
8385
{32E13EA5-C3A3-4DC9-9F2C-B938C7632320}.Release|Any CPU.Build.0 = Release|Any CPU
86+
{DF0DD259-1E2B-4385-922D-90E782C00106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87+
{DF0DD259-1E2B-4385-922D-90E782C00106}.Debug|Any CPU.Build.0 = Debug|Any CPU
88+
{DF0DD259-1E2B-4385-922D-90E782C00106}.Release|Any CPU.ActiveCfg = Release|Any CPU
89+
{DF0DD259-1E2B-4385-922D-90E782C00106}.Release|Any CPU.Build.0 = Release|Any CPU
8490
EndGlobalSection
8591
GlobalSection(SolutionProperties) = preSolution
8692
HideSolutionNode = FALSE
@@ -97,6 +103,7 @@ Global
97103
{A88078BD-72BC-42B3-B834-1DC4B54F78BE} = {5CD94193-53CA-4778-A0AC-316420C95287}
98104
{74531DF4-FF3D-437C-AE03-775F0EBBE1E4} = {5CD94193-53CA-4778-A0AC-316420C95287}
99105
{32E13EA5-C3A3-4DC9-9F2C-B938C7632320} = {5CD94193-53CA-4778-A0AC-316420C95287}
106+
{DF0DD259-1E2B-4385-922D-90E782C00106} = {5059E893-546D-4C2A-95CA-9290FA8BE064}
100107
EndGlobalSection
101108
GlobalSection(ExtensibilityGlobals) = postSolution
102109
SolutionGuid = {7A564543-B6AA-4CF1-940C-AF2C1DEFAF1A}

Creational/Singleton/Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace Singleton
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
var singleton1 = Singleton.Instance();
10+
var singleton2 = Singleton.Instance();
11+
12+
if (singleton1 == singleton2)
13+
{
14+
Console.WriteLine("Objects are the same instance!");
15+
}
16+
17+
Console.ReadKey();
18+
}
19+
}
20+
}

Creational/Singleton/Singleton.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Singleton
2+
{
3+
public sealed class Singleton
4+
{
5+
private static Singleton _instance;
6+
7+
private Singleton()
8+
{
9+
}
10+
11+
public static Singleton Instance()
12+
{
13+
return _instance ?? (_instance = new Singleton());
14+
}
15+
}
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)