Design Patterns - Singleton Pattern

I. Introduction

Design patterns are important in software development, often solutions in software development, there are many patterns followed in software development, and most companies/developers only implement one or more patterns at a time. They range from abstract factories (creating more than one instance of a class) to Singleton (only one instance of a class can exist). Today we're going to take a look at the Singleton Deasign pattern.

2. What is the Singleton Deasign model?

The Singleton pattern is a pattern that ensures that there is only one instance of a class and provides a global access point to it. The class implementing this pattern is itself responsible for keeping track of its unique instance, rather than relying on global variables. 

Third, the advantages and disadvantages of the Singleton Deasign model

It cannot be abused, use it according to the situation. Now, when designing an application, the Singleton pattern is not suitable for all cases, and if there is a situation where it needs to be used, also make sure that the class is stateless.
Suppose you are creating an application with reporting capabilities and the client wants to maintain a count of all reports generated and printed. Since the Singleton design pattern allows only one instance and it is the global access point for the reporting module, you can let the designer ensure accurate counts are always maintained without having to rely on global static variables.

4. Realization

Let's take a look at the implementation of a class using the Singleton design pattern. This Singleton example achieves thread safety by using locks. It takes the lock on the Singleton class (to make sure no race condition happens) and checks if the instance already exists. If the instance does not exist, then it creates an instance, otherwise it returns an instance that is already available, Locking ensures that a thread can create an instance.

public sealed class Singleton
{
    private static Singleton _classInstance = null;
    private static readonly object _threadSafetyLock = new object();

    Singleton() { }

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

The second way to implement the Singleton design pattern is to use the double locking technique. This technique solves the concurrency problem and avoids an exclusive lock every time the Instance property is called. This approach also allows developers to defer instantiation of an object until it is called for the first time. Let's take a look at the double locking technique when implementing the Singleton design pattern:

public sealed class Singleton
{
    static volatile Singleton _classInstance = null;
    static readonly object _threadSafetyLock = new object();

    Singleton() { }

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

Note that we marked the _classInstance attribute variable as volatile, which ensures that its value is set before being accessed. This ensures that _classInstance always contains the latest value.

This is the Singleton design pattern.

 

Original address: https://dzone.com/articles/design-patterns-singleton-c

Translated by: Guo Zheng

 

 

Author: Guo Zheng

Source: http://www.cnblogs.com/runningsmallguo/

The copyright of this article belongs to the author and the blog garden. Reprints are welcome, but this statement must be retained without the author's consent, and a link to the original text is given in an obvious position on the article page.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128862&siteId=291194637