Skip to content

Commit b627c95

Browse files
author
anupavanm
committed
Singleton Pattern example
1 parent 1ceb1a4 commit b627c95

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFactoryPattern", "A
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuilderPattern", "BuilderPattern\BuilderPattern.csproj", "{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SingletonPattern", "SingletonPattern\SingletonPattern.csproj", "{A61E958E-B6C9-413F-BACA-EA23D38A7DF2}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{A61E958E-B6C9-413F-BACA-EA23D38A7DF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{A61E958E-B6C9-413F-BACA-EA23D38A7DF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{A61E958E-B6C9-413F-BACA-EA23D38A7DF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{A61E958E-B6C9-413F-BACA-EA23D38A7DF2}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

SingletonPattern/Program.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
namespace SingletonPattern
4+
{
5+
public class President
6+
{
7+
static President instance;
8+
// Private constructor
9+
private President()
10+
{
11+
//Hiding the Constructor
12+
}
13+
14+
// Public constructor
15+
public static President get_instance()
16+
{
17+
if (instance == null) {
18+
instance = new President();
19+
}
20+
return instance;
21+
}
22+
}
23+
class Program
24+
{
25+
static void Main(string[] args)
26+
{
27+
President a = President.get_instance();
28+
President b = President.get_instance();
29+
30+
Console.WriteLine((a==b).ToString());
31+
Console.ReadLine();
32+
}
33+
}
34+
}
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>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)