Singleton design pattern

 Simple logging system implemented in C# using the Singleton design pattern:

using System; public class Logger { // Singleton instance private static Logger instance; // Lock object for thread safety private static readonly object lockObject = new object(); // Private constructor to prevent instantiation from outside private Logger() { } // Static method to get the singleton instance public static Logger GetInstance() { if (instance == null) { lock (lockObject) { if (instance == null) { instance = new Logger(); } } } return instance; } // Log methods public void LogInfo(string message) { Console.WriteLine("[INFO] " + message); } public void LogWarning(string message) { Console.WriteLine("[WARNING] " + message); } public void LogError(string message) { Console.WriteLine("[ERROR] " + message); } }

You can use this Logger class like this:

class Program { static void Main(string[] args) { Logger logger = Logger.GetInstance(); logger.LogInfo("This is an informational message."); logger.LogWarning("This is a warning message."); logger.LogError("This is an error message."); } }

Ensuring that only one instance of the Logger class exists within the application. It provides methods to log messages of different severities (info, warning, error) to the console.


Let's break down the Logger class step by step:

  1. Singleton Instance:

    csharp
    private static Logger instance;

    This static field holds the single instance of the Logger class. It is static because it should be shared across all instances of the class.

  2. Lock Object for Thread Safety:

    csharp
    private static readonly object lockObject = new object();

    This static field is used for thread safety when creating the singleton instance. It ensures that only one thread can access the critical section of code at a time.

  3. Private Constructor:

    csharp
    private Logger() { }

    The constructor is private, which means it cannot be accessed from outside the class. This prevents external code from creating instances of the Logger class.

  4. Static Method to Get Singleton Instance:

    public static Logger GetInstance() { if (instance == null) { lock (lockObject) { if (instance == null) { instance = new Logger(); } } } return instance; }

    This static method is responsible for creating and returning the singleton instance of the Logger class. It first checks if the instance is null, and if it is, it enters a lock to ensure thread safety. Inside the lock, it checks again if the instance is null and creates a new instance if it is.

  5. Log Methods:

    public void LogInfo(string message) { Console.WriteLine("[INFO] " + message); } public void LogWarning(string message) { Console.WriteLine("[WARNING] " + message); } public void LogError(string message) { Console.WriteLine("[ERROR] " + message); }

    These methods are responsible for logging messages of different severities (info, warning, error) to the console. They simply print the message along with the severity level.

The Logger class follows the Singleton design pattern, ensuring that only one instance of the class exists and providing global access to that instance. The use of a lock object ensures thread safety during the creation of the singleton instance, preventing race conditions.



Comments

Popular posts from this blog

SPFX setup & deployment

Sites.Selected | Graph API SharePoint Permission

Configure the SharePoint Online App Catalog