Design Patterns in C# Part 5 - Ensuring A Single Instance With The Singleton Pattern - by João Melo - Medium
Design Patterns in C# Part 5 - Ensuring A Single Instance With The Singleton Pattern - by João Melo - Medium
Open in app
Search
Introduction
Welcome back to the Ultimate Guide to Design Patterns in C#! In our previous posts,
we’ve explored various design patterns, including Strategy, Factory Method,
Decorator, and Observer. Today, we’ll dive into the Singleton Pattern, a creational
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-5-ensuring-a-single-instance-with-the-singleton-pattern-425facf4bd96 1/12
19/12/2024, 16:02 Design Patterns in C# Part 5: Ensuring a Single Instance with the Singleton Pattern | by João Melo | Medium
design pattern that ensures a class has only one instance and provides a global point
of access to it.
The Singleton Pattern restricts the instantiation of a class to a single object. This
pattern is useful when exactly one object is needed to coordinate actions across the
system. It ensures that a class has only one instance and provides a global point of
access to this instance.
Use the Singleton Pattern when you need to control access to shared resources, such
as configuration settings, database connections, or logging mechanisms. It is
particularly useful when the resource is expensive to create or when you want to
ensure consistent access to a resource throughout the application.
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-5-ensuring-a-single-instance-with-the-singleton-pattern-425facf4bd96 2/12
19/12/2024, 16:02 Design Patterns in C# Part 5: Ensuring a Single Instance with the Singleton Pattern | by João Melo | Medium
private ConfigurationManager()
{
// Private constructor to prevent instantiation from outside
Configuration = "Default Configuration";
}
In the Main method of the Program class, we access the singleton instance of
ConfigurationManager using the GetInstance() method. We demonstrate that
changes to the configuration are reflected across all access points by modifying the
configuration through one reference and observing the change through another
reference.
class Program
{
static void Main(string[] args)
{
// Access the singleton instance
ConfigurationManager config1 = ConfigurationManager.GetInstance();
Console.WriteLine(config1.Configuration);
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-5-ensuring-a-single-instance-with-the-singleton-pattern-425facf4bd96 3/12
19/12/2024, 16:02 Design Patterns in C# Part 5: Ensuring a Single Instance with the Singleton Pattern | by João Melo | Medium
Conclusion
In this post, we’ve explored the Singleton Pattern, a design pattern that ensures a
class has only one instance and provides a global point of access to that instance. By
using the Singleton Pattern, you can control access to shared resources and ensure
consistent state across your application. We demonstrated its use with a
configuration manager example, where centralized configuration settings are
required.
In the next post of this series, we will explore the Adapter Pattern and see how it can
help incompatible interfaces work together, enabling greater flexibility and
reusability in your code.
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-5-ensuring-a-single-instance-with-the-singleton-pattern-425facf4bd96 4/12