Skip to content

Latest commit

 

History

History
69 lines (53 loc) · 2.59 KB

File metadata and controls

69 lines (53 loc) · 2.59 KB

Singleton Design Pattern

The singleton pattern is a design pattern that ensures a class has only one instance and provides a global access point to it. The singleton class ensures that only one instance of the class is created by creating a private constructor and a static method that returns the instance.

Pros:

  • Ensures that only one instance of the class is created, which can be useful in situations where creating multiple instances would be unnecessary or resource-intensive.
  • Provides a global access point to the instance, making it easy to access the instance from anywhere in the code.

Cons:

  • Ensures that only one instance of the class is created, which can be useful in situations where creating multiple instances would be unnecessary or resource-intensive.
  • Provides a global access point to the instance, making it easy to access the instance from anywhere in the code.

When to use the singleton pattern:

  • When you want to ensure that only one instance of a class is created.
  • When you want to provide global access to the instance of a class.
  • When the singleton class will be responsible for creating and managing its own instance.

Use Case:

You are building a system for managing user accounts in an online platform. The system needs to store a list of all user accounts, and it should be accessible by multiple components of the platform. To make sure that there is only one instance of the list of user accounts, you decide to use the singleton pattern.

Implementation:

public sealed class UserAccounts
{
    private static UserAccounts _instance = null;
    private static readonly object _lock = new object();

    private List<string> _userAccounts;

    private UserAccounts()
    {
        _userAccounts = new List<string>();
    }

    public static UserAccounts GetInstance()
    {
        lock (_lock)
        {
            if (_instance == null)
            {
                _instance = new UserAccounts();
            }

            return _instance;
        }
    }

    public void AddUserAccount(string username)
    {
        _userAccounts.Add(username);
    }

    public void RemoveUserAccount(string username)
    {
        _userAccounts.Remove(username);
    }
}

To use the UserAccounts class, you would call the GetInstance() method to get the singleton instance, and then use the AddUserAccount() and RemoveUserAccount() methods to modify the list of user accounts. For example:

UserAccounts userAccounts = UserAccounts.GetInstance();
userAccounts.AddUserAccount("user1");
userAccounts.AddUserAccount("user2");
userAccounts.RemoveUserAccount("user1");