Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# Multiton Design Pattern



The Multiton pattern is a variation of the Singleton pattern. While the Singleton ensures that only one instance of a class is created, the Multiton pattern ensures that there are only a limited number of instances of a class and provides a global point of access to these instances.

Here's a detailed example in C#:

csharp
using System;
using System.Collections.Generic;

public class Multiton
{
    private static readonly Dictionary<string, Multiton> instances = new Dictionary<string, Multiton>();

    private string identifier;

    // Private constructor to prevent instantiation
    private Multiton(string id)
    {
        this.identifier = id;
    }

    public static Multiton GetInstance(string id)
    {
        if (!instances.ContainsKey(id))
        {
            instances[id] = new Multiton(id);
        }

        return instances[id];
    }

    public void DoWork()
    {
        Console.WriteLine("Instance " + identifier + " is doing work.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Multiton instance1 = Multiton.GetInstance("Instance1");
        Multiton instance2 = Multiton.GetInstance("Instance2");
        Multiton anotherInstance1 = Multiton.GetInstance("Instance1");

        instance1.DoWork();
        instance2.DoWork();
        anotherInstance1.DoWork();

        // Outputs:
        // Instance Instance1 is doing work.
        // Instance Instance2 is doing work.
        // Instance Instance1 is doing work.
    }
}

Explanation:

  1. Dictionary to store instances: The static dictionary instances keeps track of all the created instances by their identifier.

  2. Private Constructor: The constructor is private to prevent direct instantiation.

  3. GetInstance Method: This method is used to get an instance of the class. If an instance with the given identifier doesn't exist, it creates a new one and stores it in the dictionary.

  4. DoWork Method: This is a simple method to demonstrate that the instances are unique based on their identifier.

In this way, the Multiton pattern controls the number of instances and provides a way to manage them by using unique identifiers.

If you have any specific use-cases or more questions about the Multiton pattern, feel free to ask!




All rights reserved | Privacy Policy | Sitemap