Singleton
-------------------------------------------------------------------------------------------------------
UML:
--------------------------------------------------------------------------------------------------------
SAMPLE:
1 class SingletonClient
2 {
3 static void Main(string[] args)
4 {
5 Singleton singleton = Singleton.getInstance();
6 singleton.SaySomething();
7
8 // Wait for user
9 Console.ReadKey();
10 }
11 }
12
13 #region Singleton
14
15 public class Singleton
16 {
17 private volatile static Singleton _uniqueInstance;
18 private static readonly object _syncLock = new object();
19
20 private Singleton() {}
21
22 public static Singleton getInstance()
23 {
24 if (_uniqueInstance == null)
25 {
26 // Lock area where instance is created
27 lock(_syncLock)
28 {
29 if (_uniqueInstance == null)
30 {
31 _uniqueInstance = new Singleton();
32 }
33 }
34 }
35 return _uniqueInstance;
36 }
37
38 public void SaySomething()
39 {
40 Console.WriteLine("I am double checked, therefore I am");
41 }
42 }
作者:石世特
出处:http://www.cnblogs.com/TivonStone/
希望本文对你有所帮助,想转随便转,心情好的话给我的文章留个链接.o(. .)o
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步