Repository navigation
Expand file tree
/
Copy pathSIngletonPatterns.cs
More file actions
74 lines (63 loc) · 1.76 KB
/
Copy pathSIngletonPatterns.cs
File metadata and controls
74 lines (63 loc) · 1.76 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// <summary>
/// Singleton Implemented with Eager loading
/// </summary>
public class EagerLoadingSingleton
{
private static readonly EagerLoadingSingleton instance = new EagerLoadingSingleton();
private EagerLoadingSingleton() { }
public static EagerLoadingSingleton Instance => instance;
}
/// <summary>
/// SIngleton Implemented with Lazy loading
/// </summary>
public class LazyLoadingSingleton
{
private static readonly Lazy<LazyLoadingSingleton> lazyInstance = new Lazy<LazyLoadingSingleton>(() => new LazyLoadingSingleton());
private LazyLoadingSingleton() { }
public static LazyLoadingSingleton Instance => lazyInstance.Value;
}
/// <summary>
/// SIngleton Implemented with double check locking
/// </summary>
public sealed class DoubleLockingSingleton
{
private static DoubleLockingSingleton instance;
private static readonly object lockObject = new object();
private DoubleLockingSingleton() { }
public static DoubleLockingSingleton Instance
{
get
{
if (instance == null)
{
lock (lockObject)
{
if (instance == null)
{
instance = new DoubleLockingSingleton();
}
}
}
return instance;
}
}
}
/// <summary>
/// Singleton Implemented with the most basic knowledge of pattern
/// </summary>
public sealed class BasicSingleton
{
private static BasicSingleton instance;
private BasicSingleton() { }
public static BasicSingleton Instance
{
get
{
if (instance == null)
{
instance = new BasicSingleton();
}
return instance;
}
}
}