程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 單例模式(singleton),模式(singleton)

單例模式(singleton),模式(singleton)

編輯:C#入門知識

單例模式(singleton),模式(singleton)


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

 

 

後續更新

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved