软件编程
位置:首页>> 软件编程>> C#编程>> C#单例类的实现方法

C#单例类的实现方法

作者:zhouzl's blog  发布时间:2023-12-01 02:13:30 

标签:C#,单例类

单例类保证一个类全局仅有一个实例,并提供一个全局访问点,由于只能生成一个实例,因此我们必须把构造函数设为私有函数以禁止他人创建实例。

实现1:懒汉式,线程不安全

该实现没有额外开销,不要求线程安全的情况下可以使用:


public class Singleton1
{
 private static Singleton1 instance = null;
 private Singleton1() { }

public static Singleton1 Instance
 {
   get
   {
     if (instance == null)
     {
       instance = new Singleton1();
     }
     return instance;
   }
 }
}

实现2:懒汉式,线程安全

由于每次访问单例类实例都会加锁,而加锁是一个非常耗时的操作,故不推荐使用:


public class Singleton2
{
 private readonly static object lockObj = new object();
 private static Singleton2 instance = null;
 private Singleton2() { }

public static Singleton2 Instance
 {
   get
   {
     lock(lockObj)
     {
       if (instance == null)
       {
         instance = new Singleton2();
       }
     }
     return instance;
   }
 }
}

实现3:饿汉式,线程安全

写法简单,线程安全,但构造时机不是由程序员掌控的:


public class Singleton3
{
 private static Singleton3 instance = new Singleton3();
 private Singleton3() { }
 public static Singleton3 Instance { get { return instance; } }

public static void Test()
 {
   Console.WriteLine("test");
 }
}

当.NET运行时发现第一次使用Singleton3时会创建单例的实例,而不是在第一次调用Singleton3.Instance属性时创建,如进行以下操作:


Singleton3.Test();

实现4:懒汉式,双重校验锁

在实现2的基础上进行改进,只在第一次创建实例时加锁,提高访问性能:


public class Singleton4
{
 private readonly static object lockObj = new object();
 private static Singleton4 instance = null;
 private Singleton4() { }

public static Singleton4 Instance
 {
   get
   {
     if (instance == null)
     {
       lock (lockObj)
       {
         if (instance == null)
         {
           instance = new Singleton4();
         }
       }
     }
     return instance;
   }
 }
}

实现5:懒汉式,内部类

在方法3的基础上进行改进,确保只有访问Singleton5.Instance属性时才会构造实例:


public class Singleton5
{
 class Nested
 {
   internal static readonly Singleton5 instance = new Singleton5();
 }
 private Singleton5() { }
 public static Singleton5 Instance { get { return Nested.instance; } }
}

实现单例基类

通过单例基类,我们可以简单的通过继承创建一个单例类,实现代码复用:


// 由于单例基类不能实例化,故设计为抽象类
public abstract class Singleton<T> where T : class
{
 // 这里采用实现5的方案,实际可采用上述任意一种方案
 class Nested
 {
   // 创建模板类实例,参数2设为true表示支持私有构造函数
   internal static readonly T instance = Activator.CreateInstance(typeof(T), true) as T;
 }
 private static T instance = null;
 public static T Instance { get { return Nested.instance; } }
}

使用方法如下:


class TestSingleton : Singleton<TestSingleton>
{
 // 将构造函数私有化,防止外部通过new创建
 private TestSingleton() { }
}

来源:https://www.cnblogs.com/zhouzl/p/10687909.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com