📌 Introduction
In software design patterns, the Singleton Design Pattern plays a crucial role when we want to ensure that only one instance of a class exists throughout the lifecycle of an application. Singleton provides a global access point to this single instance, making it widely used in scenarios like logging, configuration management, caching, and database connections.
In this article, we will explore:
-
What is Singleton Design Pattern?
-
Importance of private constructor, static variable, and static constructor.
-
Real-time example with C#.
-
Difference between Singleton and Static class.
-
Lazy Singleton with thread safety implementation.
🚀 What is Singleton Design Pattern?
The Singleton Pattern ensures that a class has only one instance and provides a global access point to it. This prevents duplicate objects from being created unnecessarily and helps in maintaining a single state across the application.
For example, imagine a Logger class – instead of creating multiple loggers in different modules, a singleton logger ensures all logs go through one centralized object.
🔎 Importance of Components in Singleton
✅ Private Constructor
-
Prevents the creation of an object using the
new
keyword. -
Ensures external classes cannot instantiate multiple objects.
✅ Static Variable
-
Stores the single instance of the class.
-
Ensures only one instance exists globally.
✅ Static Constructor
-
Initializes the singleton instance only once, automatically by the CLR.
-
Ensures thread-safe initialization.
💡 Real-Time Example: Logger Singleton
📌 Output:
Here, Logger.Instance always points to the same object.
🆚 Singleton vs Static Class
Feature | Singleton Class | Static Class |
---|---|---|
Instance | Single object (controlled creation) | No instance (direct static access) |
Inheritance | Can implement interfaces & inherit | Cannot inherit or implement interfaces |
Flexibility | Can be extended, mocked, injected (DI) | Rigid, cannot be mocked/tested |
State Management | Can maintain state inside instance | Uses static fields (global state) |
Initialization | Can use lazy loading for performance | Always loaded at first access |
👉 Use Singleton for shared resources like Database Connections, Logger, Cache Manager.
👉 Use Static Class for utility/helper methods like Math
or Convert
.
🛡️ Lazy Singleton with Thread Safety in C#
In high-performance applications, it is better to create the singleton instance only when needed (Lazy Initialization). This saves memory and improves performance.
✅ Thread-Safe Lazy Singleton Example
📌 Output
✔ The constructor runs only once.
✔ Both config1
and config2
point to the same instance.
✔ The Lazy<T>
type ensures thread safety automatically.
🎯 Conclusion
The Singleton Design Pattern in C# is one of the most powerful and commonly used design patterns for managing shared resources. With the help of private constructor, static variables, and static constructors, we can ensure that only one instance exists throughout the application.
For modern .NET applications, the Lazy Singleton with thread safety approach is the most efficient and recommended implementation.
Key Takeaway:
-
Use Singleton when you need a shared, single instance with controlled access.
-
Use Static Class when you need utility functions without state.
No comments:
Post a Comment