-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathSingletonPatternExample3.cs
58 lines (52 loc) · 1.42 KB
/
SingletonPatternExample3.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
//-------------------------------------------------------------------------------------
// SingletonPatternExample3.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
namespace SingletonPatternExample3
{
public class SingletonPatternExample3 : MonoBehaviour
{
void Start()
{
SingletonA.Instance.DoSomething();
SingletonB.Instance.DoSomething();
}
}
public abstract class Singleton<T> where T : class, new()
{
private static readonly object _syncLock = new object();
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
lock (_syncLock)
{
if (_instance == null)
{
_instance = new T();
}
}
}
return _instance;
}
}
}
public class SingletonA : Singleton<SingletonA>
{
public void DoSomething()
{
Debug.Log("SingletonA: DoSomething!");
}
}
public class SingletonB : Singleton<SingletonB>
{
public void DoSomething()
{
Debug.Log("SingletonB: DoSomething!");
}
}
}