使用场景
来着(http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton)
- controls concurrent access to a shared resource.
- access to the resource will be requested from multiple, disparate parts of the system.
- there can be only one object.
简单模式
代码
public class BaseSingle { public void Method() { Console.WriteLine("{1}.Method:{0}", this.GetHashCode(),this.GetType().ToString()); } } public class UnsafeSingle : BaseSingle { private static UnsafeSingle _single; public static UnsafeSingle UnsafeSingleInstatce() { if (null == _single) _single = new UnsafeSingle(); return _single; } private UnsafeSingle() { Console.WriteLine(DateTime.Now.ToString() + ": 构造函数!"); } }
测试代码
internal delegate void InstanceFun(); public void GetInstatnce() { UnsafeSingle.UnsafeSingleInstatce().Method(); } public void MutilThreadTest(InstanceFun instanceFun) { var started = DateTime.Now; Thread[] threads = new Thread[100]; for (var index = 0; index < 100; index++) { Thread thread = new Thread(new ThreadStart(instanceFun)); threads[index] = thread; } foreach (var thread in threads) { thread.Start(); } Console.WriteLine("运行时长:{0}", (DateTime.Now - started).Milliseconds); } /// <summary> /// 一般单例,非线程安全 /// </summary> [Test] public void MutilUnSafeTest() { MutilThreadTest(GetInstatnce); }
缺点:对象未创建时,多线程同时访问返回的对象并非一个;
测试结果:
DesignPattern.UnsafeSingle.Method:45338574
DesignPattern.UnsafeSingle.Method:30944461
DesignPattern.UnsafeSingle.Method:62452338
DesignPattern.UnsafeSingle.Method:43921088
DesignPattern.UnsafeSingle.Method:18573725
DesignPattern.UnsafeSingle.Method:8935080
DesignPattern.UnsafeSingle.Method:48823647
DesignPattern.UnsafeSingle.Method:28613802
DesignPattern.UnsafeSingle.Method:40642054
DesignPattern.UnsafeSingle.Method:52962918
DesignPattern.UnsafeSingle.Method:29095347
DesignPattern.UnsafeSingle.Method:62694277
DesignPattern.UnsafeSingle.Method:54643837
DesignPattern.UnsafeSingle.Method:36302867
DesignPattern.UnsafeSingle.Method:49913678
DesignPattern.UnsafeSingle.Method:52482287
DesignPattern.UnsafeSingle.Method:41829035
DesignPattern.UnsafeSingle.Method:57978397
DesignPattern.UnsafeSingle.Method:13456355
DesignPattern.UnsafeSingle.Method:23736788
DesignPattern.UnsafeSingle.Method:42195492
DesignPattern.UnsafeSingle.Method:28113429
DesignPattern.UnsafeSingle.Method:2875151
DesignPattern.UnsafeSingle.Method:56858914
DesignPattern.UnsafeSingle.Method:2735313
DesignPattern.UnsafeSingle.Method:50717245
DesignPattern.UnsafeSingle.Method:30697644
DesignPattern.UnsafeSingle.Method:47515266
DesignPattern.UnsafeSingle.Method:30183720
DesignPattern.UnsafeSingle.Method:65195544
DesignPattern.UnsafeSingle.Method:22695823
DesignPattern.UnsafeSingle.Method:8561069
DesignPattern.UnsafeSingle.Method:33135541
DesignPattern.UnsafeSingle.Method:17719913
DesignPattern.UnsafeSingle.Method:32493892
DesignPattern.UnsafeSingle.Method:57339132
DesignPattern.UnsafeSingle.Method:38516275
DesignPattern.UnsafeSingle.Method:33858004
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
……
运行时长:115
DesignPattern.UnsafeSingle.Method:58042889