knowledge/technology/programming/patterns/creational/Multiton Pattern.md
2023-12-04 11:02:23 +01:00

3.1 KiB

obj
concept

Multiton Pattern

Description:

The Multiton Pattern is a variation of the Singleton Pattern that ensures a class has only a limited number of instances, where each instance is uniquely identified by a key. Unlike the Singleton Pattern, which allows only one instance of a class, the Multiton Pattern allows multiple instances, each associated with a specific key.

How it's Used:

  1. Multiton Class: Define a class that you want to limit the number of instances for. This class will manage multiple instances, each associated with a unique key.
  2. Private Constructor: Ensure that the constructor of the class is private to prevent external instantiation.
  3. Internal Registry: Create an internal registry (usually a dictionary or map) within the class to store instances associated with their respective keys.
  4. Factory Method: Implement a factory method or a static method within the class to create or retrieve instances based on keys.
  5. Use Keys: When you need an instance of the class, provide a key to the factory method. The method will either create a new instance or return an existing instance associated with the provided key.

Example:

Let's say you're creating a logging system for your application, and you want to have separate loggers for different modules. You can use the Multiton Pattern to ensure that each module has its own logger:

  • Multiton Class: Define a Logger class.
public class Logger {
    private static final Map<String, Logger> instances = new HashMap<>();

    private Logger() {
        // Private constructor
    }

    public static Logger getInstance(String module) {
        if (!instances.containsKey(module)) {
            instances.put(module, new Logger());
        }
        return instances.get(module);
    }

    public void log(String message) {
        // Logging implementation
        System.out.println(message);
    }
}
  • Private Constructor: Ensure that the constructor of the Logger class is private to prevent external instantiation.
  • Internal Registry: Use a Map<String, Logger> (instances) to store logger instances associated with their respective modules (keys).
  • Factory Method: Implement a getInstance static method that takes a module parameter and returns a Logger instance associated with that module. If an instance for the module does not exist, create one and store it in the registry.
  • Use Keys: In your application, when you need a logger for a specific module, use the Logger.getInstance(module) method to obtain or create the logger instance.
Logger loggerA = Logger.getInstance("ModuleA");
Logger loggerB = Logger.getInstance("ModuleB");

loggerA.log("Log message for ModuleA");
loggerB.log("Log message for ModuleB");

By using the Multiton Pattern, you ensure that each module gets its own unique logger instance, identified by the module name. This allows you to have separate loggers for different parts of your application while keeping the logging code centralized and manageable.