-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingletonLearning.cs
89 lines (60 loc) · 1.71 KB
/
SingletonLearning.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Singleton
{
internal class SingletonLearning
{
}
//Everytime we try to get instance we get same instance is known as singleton in this we have to create its
// constructor as private
// lets see
public class ChocolateMachine
{
private static ChocolateMachine _instance;
// Private constructor so no one can create another machine directly.
private ChocolateMachine() { }
// Method to get the single instance of the ChocolateMachine.
public static ChocolateMachine GetInstance()
{
if (_instance == null)
{
_instance = new ChocolateMachine();
}
return _instance;
}
public void MakeChocolate()
{
Console.WriteLine("Chocolate is being made!");
}
}
public class Learn_GetSingleton
{
private static Learn_GetSingleton _instance;
private static int instance_counter = 0;
private Learn_GetSingleton()
{
instance_counter++;
}
public static Learn_GetSingleton GetInstance()
{
if (_instance == null)
{
return new Learn_GetSingleton();
}
return _instance;
}
public void testFunction()
{
Console.WriteLine("getting same instance");
}
public static int GetCounter()
{
return instance_counter;
}
}
}