-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingletonExample.cs
More file actions
52 lines (49 loc) · 1.78 KB
/
SingletonExample.cs
File metadata and controls
52 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DesignPattern
{
// Define the SingletonExample class
public class SingletonExample
{
// Define the static Run method within the SingletonExample class
public static void Run()
{
// Create a variable to hold the instance of the Singleton class
var instance = Singleton.Instance;
// Call the DoSomething method on the Singleton instance
instance.DoSomething();
}
}
// Define the Singleton class
public class Singleton
{
// Private static field to hold the single instance of the Singleton class
private static Singleton _instance;
// Private constructor to prevent external instantiation of the class
private Singleton() { }
// Public static property to provide access to the single instance of the Singleton class
public static Singleton Instance
{
get
{
// Check if the _instance field is null (i.e., the Singleton instance has not been created yet)
if (_instance == null)
{
// If _instance is null, create a new instance of the Singleton class
_instance = new Singleton();
}
// Return the single instance of the Singleton class
return _instance;
}
}
// Public method to perform some action
public void DoSomething()
{
// Print a message to the console indicating that the Singleton instance is performing an action
Console.WriteLine("Singleton instance is doing something!");
}
}
}