0% found this document useful (0 votes)
27 views4 pages

Design Patterns in C# Part 5 - Ensuring A Single Instance With The Singleton Pattern - by João Melo - Medium

This document discusses the Singleton Pattern in C#, which ensures that a class has only one instance and provides global access to it. It explains when to use the pattern, particularly for managing shared resources like configuration settings, and provides a real-world example with a ConfigurationManager class. The article also outlines the implementation details and demonstrates how to access the singleton instance in client code.

Uploaded by

ekrrmerder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Design Patterns in C# Part 5 - Ensuring A Single Instance With The Singleton Pattern - by João Melo - Medium

This document discusses the Singleton Pattern in C#, which ensures that a class has only one instance and provides global access to it. It explains when to use the pattern, particularly for managing shared resources like configuration settings, and provides a real-world example with a ConfigurationManager class. The article also outlines the implementation details and demonstrates how to access the singleton instance in client code.

Uploaded by

ekrrmerder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

19/12/2024, 16:02 Design Patterns in C# Part 5: Ensuring a Single Instance with the Singleton Pattern | by João Melo | Medium

Open in app

Search

Your membership will expire on January 11, 2025 Reactivate membership

Design Patterns in C# Part 5: Ensuring a Single


Instance with the Singleton Pattern
João Melo · Follow
3 min read · Aug 3, 2024

Listen Share More

Mastering Design Patterns in C#: A Comprehensive Series

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.

💡Type — Creational patterns deal with object creation mechanisms, trying to


create objects in a manner suitable to the situation. They help make a system
independent of how its objects are created, composed, and represented.

What is the Singleton Pattern?

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.

When to Use the Singleton Pattern?

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.

Real-world example in C# — Configuration Manager

Imagine we are developing an application that requires centralized configuration


management. Instead of creating multiple instances of a configuration manager, we
can use the Singleton Pattern to ensure there is only one instance that provides
consistent configuration settings across the application.

Step 1: Create the Singleton Class

The ConfigurationManager class demonstrates a basic Singleton implementation. It


has a private static field _instance to hold the single instance and a static readonly
object _lock for thread safety. The constructor is private to prevent external
instantiation. The GetInstance() method ensures that only one instance of the class
is created using a double-check locking mechanism to handle multithreading
scenarios.

public class ConfigurationManager


{

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 static ConfigurationManager _instance;


private static readonly object _lock = new object();
public string Configuration { get; set; }

private ConfigurationManager()
{
// Private constructor to prevent instantiation from outside
Configuration = "Default Configuration";
}

public static ConfigurationManager GetInstance()


{
// Double-check locking mechanism for thread safety
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new ConfigurationManager();
}
}
}
return _instance;
}
}

Step 2: Access the Singleton Instance in Client Code

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);

// Modify the configuration


config1.Configuration = "Updated 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

// Access the singleton instance from another place


ConfigurationManager config2 = ConfigurationManager.GetInstance();
Console.WriteLine(config2.Configuration);
}
}

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.

this is what your project tells your singleton

Design Patterns Csharp Csharp Programming Net Core Singleton

https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-5-ensuring-a-single-instance-with-the-singleton-pattern-425facf4bd96 4/12

You might also like