熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

單例類助手(代碼)

2013-11-13 10:26:39  來源: .NET編程 

  /// <summary>

  /// 單例類助手

  /// </summary>

  public sealed class SingletonHelper<T> where T : new()

  {

  private static T instance = new T();

  private static object locker = new object();

  private SingletonHelper() { }

  /// <summary>

  /// 獲取單例

  /// </summary>

  /// <returns></returns>

  public static T GetInstance()

  {

  if (null == instance)

  {

  lock (locker)

  {

  if (null == instance)

  {

  instance = new T();

  }

  }

  }

  return instance;

  }

  /// <summary>

  /// 設置單例

  /// </summary>

  /// <param name=value></param>

  public void SetInstance(T value)

  {

  instance = value;

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/13636.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.