码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式(singleton)

时间:2016-08-03 15:21:41      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

1. 使用场景    

  来着stackorverflow  

  • 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.
  • from (http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton)

2. 简单模式

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);
}

  测试结果:  

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
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
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

 

 

后续更新

单例模式(singleton)

标签:

原文地址:http://www.cnblogs.com/webbdang/p/5732863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!