C# Singleton
C# Singleton
Summary: in this tutorial, you will learn how to use the C# Singleton pattern to ensure that a
class has only one instance.
Sometimes, you need to create one and only one instance of a class. The reason is that creating
multiple instances of such a class may lead to problems like incorrect data or resource
contention. For example, when you work with a logging framework or database connection.
The Singleton pattern solves the problem of creating a single instance of a class that should be
globally accessible throughout the application.
By definition, the Singleton pattern is a creational design pattern that allows a class to have only
one instance and provides a global point of access to that instance.
- instance: Singleton
Client Use
- Singleton()
+Instance(): Singleton
The Singleton pattern consists of a single class that is responsible for creating and maintaining a
single instance.
The Singleton class is responsible for creating a single instance. The Client class gets the
Singleton instance by calling the Instance accessor or GetInstance() method.
The Singleton class has a constructor marked as private. This prevents other classes from
creating instances of the Singleton class.
The Singleton class also has an accessor that provides access to the instance.
Besides the above benefits, the Singleton pattern has the following drawbacks:
Difficult to test. The Singleton pattern can make testing more difficult. Because the
Singleton pattern creates a global point of access to the instance, which leads to
unexpected dependencies in test cases.
Lead to tight coupling between classes. Classes that depend on the Singleton instance are
aware of its existence and how to access it, which may lead to a strong coupling between
the classes. As a result, it’s difficult to change the Singleton class or the classes that depend
on the Singleton class without affecting other parts of the system.
Singleton implementation in C#
The following illustrates how to use implement the Singleton pattern in C#:
private Singleton()
{
}
}
How it works.
First, define the Singleton class with the sealed keyword so that other classes will not be
able to inherit from this class.
Second, declare a static private field with the type Singleton called _instance .
Third, define the accessor that creates an instance of the Singleton class if it is null ;
otherwise, return the instance. This accessor is the only way to access the Singleton instance.
Finally, mark the constructor of the Singleton class as private to prevent other classes from
creating a new instance of the Singleton class.
C# Singleton example
The following program demonstrates how to apply the Singleton pattern to define a Logger
class that has one and only one instance throughout the application:
namespace DP;
private Logger()
{
}
class Program
{
public static void Main(string[] args)
{
var logger = Logger.Instance;
logger.Log("C# Singleton");
}
}
Output:
C# Singleton
Singleton applications
Logging and auditing frameworks that need to maintain a single log or audit trail throughout
the system.
Configuration management, where you need one and only one instance of the configuration
manager that is accessible from any part of the system.
Summary
Use the Singleton pattern to ensure that a class has one and only one instance through the
application.
The Singleton can make the application difficult to test and may lead to tight coupling
between classes.