当前位置: 移动技术网 > IT编程>开发语言>.net > C#单例类的实现

C#单例类的实现

2019年04月12日  | 移动技术网IT编程  | 我要评论

wowbox,乐嘉自曝初夜,鱼饵秘方

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() { }
}

参考资料

1、《剑指offer》

2、

3、

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网