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

singleton pattern的推荐实现

时间:2014-07-13 17:53:37      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   使用   

一、单例模式的C#实现:

(1)使用double-checked locking的方式:

public sealed class Singleton
{
    static Singleton instance = null;
    static readonly object padlock = new object();

    Singleton()
    {
    }

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

这种实现方式对多线程来说是安全的,同时线程不是每次都加锁,只有判断对象实例没有被创建时它才加锁。

因为使用了锁,没有下面的方式高效。

(2)使用静态成员初始化方式(thread-safe without using locks)

public sealed class Singleton
{
    static readonly Singleton instance = new Singleton();

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton()
    {
    }

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}

由于Instance是static的,保证在AppDomain只有一个实例。

初始化的线程安全性是由.Net保证的。

手动加上一个static的constructor,以让.Net对它进行延迟加载,但并不是完全延迟初始化,当该单例有其他static成员被使用时,该instance就被创建,而不是该instance被使用时才被创建。

(3)推荐方式:使用内部一个Instance的Holder来“持有”单例

public sealed class Singleton
{
    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    
    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

这种实现方式不需要加锁(without using locks)。

这种方式是完全延迟初始化(fully lazy instantiation),该Instance只有被使用时才被创建。

二、单例模式的Java实现:

(1)使用double-checked locking的方式:

public class SingletonDemo {

    private static volatile SingletonDemo instance = null;

    private SingletonDemo() { }

    public static SingletonDemo getInstance() {
        if (instance == null) {
            synchronized (SingletonDemo.class) {
                if (instance == null) {
                    instance = new SingletonDemo();
                }
            }
        }
        return instance;
    }
}

(2)使用Eager initialization的方式:

public class Singleton 
{
    private static final Singleton instance = new Singleton();
 
    private Singleton() {}
 
    public static Singleton getInstance() {
        return instance; 
    }
}

(3)推荐实现:Initialization On Demand Holder Idiom

public class Singleton 
{
    // Private constructor prevents instantiation from other classes
    private Singleton() { }
 
    /**
    * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
    * or the first access to SingletonHolder.INSTANCE, not before.
    */
    private static class SingletonHolder { 
        private static final Singleton instance = new Singleton();
    }
 
    public static Singleton getInstance() {
        return SingletonHolder.instance;
    }
}

 

 

参考:

http://en.wikipedia.org/wiki/Singleton_pattern

http://www.yoda.arachsys.com/csharp/singleton.html

 

singleton pattern的推荐实现,布布扣,bubuko.com

singleton pattern的推荐实现

标签:style   blog   http   java   color   使用   

原文地址:http://www.cnblogs.com/luxiaoxun/p/3841223.html

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