Singleton Design Pattern

The Singleton pattern is one of the simplest design patterns, which restricts the instantiation of a class to ONLY ONE object. A singleton class only allows a single instance of itself to be created, and usually gives simple access to that instance. Most commonly, singletons don‘t allow any parameters to be specified when creating the instance.

In this article, we would provide different ways of implementing the singleton pattern using C# and discuss the difference in terms of lazily-load, thread-safety, and perfromance.


Singleton and Static Class

Before describing the implementations, we clarify the difference between singletons and static classes. We can make a global single class by using keyword static. Both singleton and static class have only one instance of them. The static classes are usually used for storing global data, all the data in a static class are also static. A singleton is treated as a normal class (without static keyword) with state, which allows you to reuse code and control object state much easier. We can extend classes or implement interfaces with singletons but not with static classes. Aslo, a singleton is allocated in heap and a static class is allocated in stack.

Instantiation: Lazy vs Eager

// Implementation 1: lazy instantiation, not thread-safe! Do not use!
public class Singleton1
{
    private static Singleton1 mInstance = null;

    // Private constructor
    private Singleton1() { }

    // GetInstance
    public static Singleton1 Instance
    {
        get
        {
            if (mInstance==null)
            {
                mInstance = new Singleton1();
            }
            return mInstance;
        }
    }
} 

// Implementation 2: eager instantiation, simple thread-safe without using locks
public sealed class Singleton2
{
    private static readonly Singleton2 mInstance = new Singleton2();

    // Private constructor
    private Singleton2() { }

    // GetInstance
    public static Singleton2 Instance
    {
        get
        {
            return mInstance;
        }
    }
}

The Singleton1 implementation is bad because it is not thread-safe. Two different threads could both pass the if test when the mInstance is null, then both of them create instances, which violates the singleton pattern. The advantage of this implementation is the instance is created inside the Instance property method, the class can exercise additional functionality. The instantiation is not performed until an object asks for an instance, so called "Lazy Instantiation", the lazy instantiation avoids instantiating unnecessary singletons when the application starts.

The Singleton2 implementation is an eager initialization, which will always create an instance. In this implementation, the instance is created the first time any member of the class is referenced, the common language runtime takes care of the variable initialization. The class is marked sealed to prevent derivation, which could add instances. The instance variable is marked readonly which means that it can be assigned only during static initialization or in a class constructor. Hence, the *private* constructor ensures that the instance variable can be instantiated only inside the the class and therefore only one instance can exist in the system. The downside of this implementation is that you have less control over the mechanics of the instantiation.

Thread-safety

// Implementation 3: thread-safe with locking the shared object
public sealed class Singleton3
{
    private static Singleton3 mInstance = null;
    private static readonly object SingletonLock = new object();

    Singleton3() {   }

    public static Singleton3 Instance
    {
        get
        {
            lock (SingletonLock)
            {
                if (mInstance == null)
                {
                    mInstance = new Singleton();
                }
                return mInstance;
            }
        }
    }
}

In the Singleton3 implementation, all threads share a lock object to ensure that only one thread will create an instance, because only the first thread entering in the critical section can find the instance variable is null and pass the if check. The withdraw of this implementation is obvious, the performance suffers as a lock is acquired every time the instance is requested.

The Singleton4 implementation improve the performance by avoiding the unnecessary lock operator. To do so, it uses a double null-check to avoid taking out a lock every time. However, this implementation doesn‘t work in Java and is easy to get wrong.

// Implementation 4: double null-check. Bad code! Do not use!
public sealed class Singleton4
{
    private static Singleton4 mInstance = null;
    private static readonly object SingletonLock = new object();

    Singleton()
    {
    }

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

Laziness and Performance

To be fully lazy instantiated, Singleton5 implementation uses a nested class in the singleton class. Therefore, the instantiation is triggered the first time the nested class is referenced which could only occur in Instance. This implementation is fully lazy and has better performance than previous implementations.

// Implementation5: fully lazy using nested classpublic sealed class Singleton5
{
    private Singleton() {    }

    public static Singleton5 Instance
    {
        get
        {
            return Nested.instance;
        }
    }

    private class Nested
    {
        static Nested()
        {
        }
        // Make the instantiation
        internal static readonly Singleton instance = new Singleton5();
    }
}

If you‘re using .NET 4 or higher, you can use the System.Lazy<T> type to make the laziness really simple. As shown in Singleton6 implementation, all you need to do is pass a delegate to the constructor which calls the Singleton constructor - which is done most easily with a lambda expression. It‘s simple and performs well. Aslo, you can check whether the instance has been created with the IsValueCreated property.

// Implementation6: fully lazy, .NET 4 or higher only
public sealed class Singleton6
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton6());

    private Singleton() {   }

    public static Singleton Instance
    {
        get
        {
            return lazy.Value;
        }
    }
}


References:

http://csharpindepth.com/articles/general/singleton.aspx
http://msdn.microsoft.com/en-us/library/ff650316.aspx

时间: 2024-10-24 12:31:32

Singleton Design Pattern的相关文章

Design Pattern Singleton 单一模式

单一模式的几个注意点: 一) 设计单一模式,首先需要把构造函数给私有化了,不让外界访问,那么外界只能通过提供的函数获取一个新的类. 二) C++的单一模式,记得要在类外初始化一个类,否则或内存出错的. 三) 这个唯一的类必须是要静态的 程序: #ifndef _SINGLETON_H #define _SINGLETON_H #include <iostream> #include <string> using namespace std; class DuGuJiuJian {

Design Pattern —— Singleton

Design Pattern —— Singleton   强力推荐枚举和类级内部类方式实现单例模式 单例模式是开发中非常常用的一种模式,简单的说,我们希望一个类永远都只有一个对象. 主要有两个用途: 1.存储一些进程内共享的值(不是很推荐,大部分情况下还是应该用局部变量,互相传递值的方式) 2.任何时候都不变的操作 单例模式的实现目前已知的有五种: 1.饿汉式 2.懒汉式 3.双重验证 4.类级内部类 5.枚举 一.饿汉式 类加载时就创建好对象,以空间换时间.这样外部调用EagerSingle

[Design Pattern] Singleton Pattern 简单案例

Singleton Pattern, 即单例模式,用于获取类的一个对象,该对象在整个应用中是其类的唯一对象.单例模式属于创建类的设计模式. SingleObject 作为单例类,内含了一个静态私有的 SingleObject 成员变量,将构造方法私有化使得外部无法构造 SingleObject 的对象,同时提供一个公共的 getInstance 方法,提供唯一的对象. 代码实现: 单例类 SingleObject public class SingleObject { private stati

Head First Design Pattern 读书笔记(4) 单例模式

Head First Design Pattern 读书笔记(4) Singleton Pattern 单例模式 Silngleton Pattern 类图 单例模式非常简单,基本没有什么类之间的关系,就不画图了,保证某个类生成的实例只有一个即可. 定义 单例模式:只允许一个类实例化一个对象,并提供一个可以全局访问这个实例的方法(接口). 关于单例模式 最简单的单例模式实现方法是: /* *"懒汉式"写法,即等要用了再去实例化实例对象,而不是应用一启动就实例化好放在容器中. */ pu

C++ Design Pattern: What is a Design Pattern?

Q: What is a Design Pattern? A: Design Patterns represent solutions to problems what arise when developing software within a particular context. Each pattern describes a problem which occurs over and over again in our environment, and then describes

设计模式(Design pattern)概述

设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结, 使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性 设计框架 可复用面向对象软件系统一般划分为两大类:应用程序工具箱和框架(Framework) 我们平时开发的具体软件都是应用程序 而框架是构成一类特定软件可复用设计的一组相互协作的类 框架通常定义了应用体系的整体结构类和对象的关系等等设计参数,以便于具体应用实现者能集中精力于应用本身的特定细节. 框架主要记录软件应用中

Null Object Design Pattern (Python recipe)

Null Object 个人感觉非常有用.也是在review公司其他同事写代码的时候看到. 当时使用了flask的request全局请求变量g,然后使用了g.x保存了一个东西. 当时在view代码读取g.x的时候震惊了,因为这一段代码并没有保存g.x,按道理来说应该是一个空值,当我拿着空值去调用其属性的时候应该会报AttributeError. 但是什么也没有发生,既没有报错,也没有发生什么,而且对其判断还是False,于是查看其实现才发现了这个.以下全部转自http://code.active

DP什么意思 design pattern 设计模式

DP  design pattern 大话设计模式  中的DP 是设计模式的意思 设计模式的书 ,最经典最原始的就是 GOF 的<设计模式>了. 设计模式的书基本上大多是以这 20 多个模式分开讲.含<大话设计模式> 学了 OOL 写的程序基本上是 OB 的. 只有慢慢掌握了 DP 才能写出真正的 OO 程序. 思想 -> 设计原则 -> DP -> OOD

Head First Design Pattern 读书笔记(2) 观察者模式

Head First Design Pattern 读书笔记(2) Observer Pattern 观察者模式 Observer Pattern 类图 定义 观察者模式:在对象间定义一个一对多的关系,当其中一个的对象发生改变时,所有依赖于这个对象的对象(即观察者们)都会自动更新或做执行某些行为. 几个OO的原测 尽量以松耦合的方式处理对象间关系–>软件工程时候学的"高內聚,低耦合"的好处 关于观察者模式 被观察对象通知观察者可以使用推送的方式(类图中带参数的notifyActi