|
| 1 | +--- |
| 2 | +title: Multiton |
| 3 | +category: Creational |
| 4 | +language: en |
| 5 | +tag: |
| 6 | + - Decoupling |
| 7 | + - Instantiation |
| 8 | + - Object composition |
| 9 | +--- |
| 10 | + |
| 11 | +## Also known as |
| 12 | + |
| 13 | +* Registry of Singletons |
| 14 | + |
| 15 | +## Intent |
| 16 | + |
| 17 | +The Multiton pattern is a variation of the Singleton design pattern that manages a map of named instances as key-value pairs. |
| 18 | + |
| 19 | +## Explanation |
| 20 | + |
| 21 | +Real-world example |
| 22 | + |
| 23 | +> A real-world example of the Multiton pattern is a printer management system in a large office. In this scenario, the office has several printers, each serving a different department. Instead of creating a new printer object every time a printing request is made, the system uses the Multiton pattern to ensure that each department has exactly one printer instance. When a printing request comes from a specific department, the system checks the registry of printer instances and retrieves the existing printer for that department. If no printer exists for that department, it creates one, registers it, and then returns it. This ensures efficient management of printer resources and prevents unnecessary creation of multiple printer instances for the same department. |
| 24 | +
|
| 25 | +In plain words |
| 26 | + |
| 27 | +> Multiton pattern ensures there are a predefined amount of instances available globally. |
| 28 | +
|
| 29 | +Wikipedia says |
| 30 | + |
| 31 | +> In software engineering, the multiton pattern is a design pattern which generalizes the singleton pattern. Whereas the singleton allows only one instance of a class to be created, the multiton pattern allows for the controlled creation of multiple instances, which it manages through the use of a map. |
| 32 | +
|
| 33 | +**Programmatic Example** |
| 34 | + |
| 35 | +The Nazgûl, also called ringwraiths or the Nine Riders, are Sauron's most terrible servants. By definition, there's always nine of them. |
| 36 | + |
| 37 | +`Nazgul` is the multiton class. |
| 38 | + |
| 39 | +```java |
| 40 | +public enum NazgulName { |
| 41 | + |
| 42 | + KHAMUL, MURAZOR, DWAR, JI_INDUR, AKHORAHIL, HOARMURATH, ADUNAPHEL, REN, UVATHA |
| 43 | +} |
| 44 | + |
| 45 | +public final class Nazgul { |
| 46 | + |
| 47 | + private static final Map<NazgulName, Nazgul> nazguls; |
| 48 | + |
| 49 | + @Getter |
| 50 | + private final NazgulName name; |
| 51 | + |
| 52 | + static { |
| 53 | + nazguls = new ConcurrentHashMap<>(); |
| 54 | + nazguls.put(NazgulName.KHAMUL, new Nazgul(NazgulName.KHAMUL)); |
| 55 | + nazguls.put(NazgulName.MURAZOR, new Nazgul(NazgulName.MURAZOR)); |
| 56 | + nazguls.put(NazgulName.DWAR, new Nazgul(NazgulName.DWAR)); |
| 57 | + nazguls.put(NazgulName.JI_INDUR, new Nazgul(NazgulName.JI_INDUR)); |
| 58 | + nazguls.put(NazgulName.AKHORAHIL, new Nazgul(NazgulName.AKHORAHIL)); |
| 59 | + nazguls.put(NazgulName.HOARMURATH, new Nazgul(NazgulName.HOARMURATH)); |
| 60 | + nazguls.put(NazgulName.ADUNAPHEL, new Nazgul(NazgulName.ADUNAPHEL)); |
| 61 | + nazguls.put(NazgulName.REN, new Nazgul(NazgulName.REN)); |
| 62 | + nazguls.put(NazgulName.UVATHA, new Nazgul(NazgulName.UVATHA)); |
| 63 | + } |
| 64 | + |
| 65 | + private Nazgul(NazgulName name) { |
| 66 | + this.name = name; |
| 67 | + } |
| 68 | + |
| 69 | + public static Nazgul getInstance(NazgulName name) { |
| 70 | + return nazguls.get(name); |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +And here's how we access the `Nazgul` instances. |
| 76 | + |
| 77 | +```java |
| 78 | + public static void main(String[] args) { |
| 79 | + // eagerly initialized multiton |
| 80 | + LOGGER.info("Printing out eagerly initialized multiton contents"); |
| 81 | + LOGGER.info("KHAMUL={}", Nazgul.getInstance(NazgulName.KHAMUL)); |
| 82 | + LOGGER.info("MURAZOR={}", Nazgul.getInstance(NazgulName.MURAZOR)); |
| 83 | + LOGGER.info("DWAR={}", Nazgul.getInstance(NazgulName.DWAR)); |
| 84 | + LOGGER.info("JI_INDUR={}", Nazgul.getInstance(NazgulName.JI_INDUR)); |
| 85 | + LOGGER.info("AKHORAHIL={}", Nazgul.getInstance(NazgulName.AKHORAHIL)); |
| 86 | + LOGGER.info("HOARMURATH={}", Nazgul.getInstance(NazgulName.HOARMURATH)); |
| 87 | + LOGGER.info("ADUNAPHEL={}", Nazgul.getInstance(NazgulName.ADUNAPHEL)); |
| 88 | + LOGGER.info("REN={}", Nazgul.getInstance(NazgulName.REN)); |
| 89 | + LOGGER.info("UVATHA={}", Nazgul.getInstance(NazgulName.UVATHA)); |
| 90 | + |
| 91 | + // enum multiton |
| 92 | + LOGGER.info("Printing out enum-based multiton contents"); |
| 93 | + LOGGER.info("KHAMUL={}", NazgulEnum.KHAMUL); |
| 94 | + LOGGER.info("MURAZOR={}", NazgulEnum.MURAZOR); |
| 95 | + LOGGER.info("DWAR={}", NazgulEnum.DWAR); |
| 96 | + LOGGER.info("JI_INDUR={}", NazgulEnum.JI_INDUR); |
| 97 | + LOGGER.info("AKHORAHIL={}", NazgulEnum.AKHORAHIL); |
| 98 | + LOGGER.info("HOARMURATH={}", NazgulEnum.HOARMURATH); |
| 99 | + LOGGER.info("ADUNAPHEL={}", NazgulEnum.ADUNAPHEL); |
| 100 | + LOGGER.info("REN={}", NazgulEnum.REN); |
| 101 | + LOGGER.info("UVATHA={}", NazgulEnum.UVATHA); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Program output: |
| 106 | + |
| 107 | +``` |
| 108 | +15:16:10.597 [main] INFO com.iluwatar.multiton.App -- Printing out eagerly initialized multiton contents |
| 109 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- KHAMUL=com.iluwatar.multiton.Nazgul@4141d797 |
| 110 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- MURAZOR=com.iluwatar.multiton.Nazgul@38cccef |
| 111 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- DWAR=com.iluwatar.multiton.Nazgul@5679c6c6 |
| 112 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- JI_INDUR=com.iluwatar.multiton.Nazgul@27ddd392 |
| 113 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- AKHORAHIL=com.iluwatar.multiton.Nazgul@19e1023e |
| 114 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- HOARMURATH=com.iluwatar.multiton.Nazgul@7cef4e59 |
| 115 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- ADUNAPHEL=com.iluwatar.multiton.Nazgul@64b8f8f4 |
| 116 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- REN=com.iluwatar.multiton.Nazgul@2db0f6b2 |
| 117 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- UVATHA=com.iluwatar.multiton.Nazgul@3cd1f1c8 |
| 118 | +15:16:10.600 [main] INFO com.iluwatar.multiton.App -- Printing out enum-based multiton contents |
| 119 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- KHAMUL=KHAMUL |
| 120 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- MURAZOR=MURAZOR |
| 121 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- DWAR=DWAR |
| 122 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- JI_INDUR=JI_INDUR |
| 123 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- AKHORAHIL=AKHORAHIL |
| 124 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- HOARMURATH=HOARMURATH |
| 125 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- ADUNAPHEL=ADUNAPHEL |
| 126 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- REN=REN |
| 127 | +15:16:10.601 [main] INFO com.iluwatar.multiton.App -- UVATHA=UVATHA |
| 128 | +``` |
| 129 | + |
| 130 | +## Applicability |
| 131 | + |
| 132 | +Use the Multiton pattern when |
| 133 | + |
| 134 | +* A class must have named instances, but only one instance for each unique key. |
| 135 | +* Global access to these instances is necessary without requiring global variables. |
| 136 | +* You want to manage shared resources categorized by key. |
| 137 | + |
| 138 | +## Known Uses |
| 139 | + |
| 140 | +* Managing database connections in different contexts. |
| 141 | +* Configuration settings for different environments in an application. |
| 142 | + |
| 143 | +## Consequences |
| 144 | + |
| 145 | +Benefits: |
| 146 | + |
| 147 | +* Ensures controlled access to instances based on key. |
| 148 | +* Reduces global state usage by encapsulating instance management within the pattern. |
| 149 | + |
| 150 | +Trade-offs: |
| 151 | + |
| 152 | +* Increased memory usage if not managed properly due to multiple instances. |
| 153 | +* Potential issues with concurrency if not implemented with thread safety in mind. |
| 154 | + |
| 155 | +## Related Patterns |
| 156 | + |
| 157 | +* [Singleton](https://java-design-patterns.com/patterns/singleton/): Multiton can be seen as an extension of the Singleton pattern where Singleton allows only one instance of a class, Multiton allows one instance per key. |
| 158 | +* [Factory Method](https://java-design-patterns.com/patterns/factory-method/): Multiton uses a method to create or retrieve instances, similar to how a Factory Method controls object creation. |
| 159 | + |
| 160 | +## Credits |
| 161 | + |
| 162 | +* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI) |
0 commit comments