Skip to content

Commit 3b67748

Browse files
committed
recommended changes commited
1 parent b65c96c commit 3b67748

File tree

11 files changed

+536
-8
lines changed

11 files changed

+536
-8
lines changed

multiton/bin/README.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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)

multiton/bin/etc/multiton.png

10.4 KB
Loading

multiton/bin/etc/multiton.ucls

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
4+
<enumeration id="1" language="java" name="com.iluwatar.NazgulName" project="multiton"
5+
file="/multiton/src/main/java/com/iluwatar/NazgulName.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="232" y="516"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</enumeration>
13+
<class id="2" language="java" name="com.iluwatar.Nazgul" project="multiton"
14+
file="/multiton/src/main/java/com/iluwatar/Nazgul.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="231" y="279"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<association id="3">
23+
<end type="SOURCE" refId="2" navigable="false">
24+
<attribute id="4" name="name"/>
25+
<multiplicity id="5" minimum="0" maximum="1"/>
26+
</end>
27+
<end type="TARGET" refId="1" navigable="true"/>
28+
<display labels="true" multiplicity="true"/>
29+
</association>
30+
<association id="6">
31+
<end type="SOURCE" refId="2" navigable="false">
32+
<attribute id="7" name="nazguls"/>
33+
<multiplicity id="8" minimum="0" maximum="2147483647"/>
34+
</end>
35+
<end type="TARGET" refId="2" navigable="true"/>
36+
<display labels="true" multiplicity="true"/>
37+
</association>
38+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
39+
sort-features="false" accessors="true" visibility="true">
40+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
41+
<operations public="true" package="true" protected="true" private="true" static="true"/>
42+
</classifier-display>
43+
<association-display labels="true" multiplicity="true"/>
44+
</class-diagram>

multiton/bin/etc/multiton.urm.puml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@startuml
2+
package com.iluwatar.multiton {
3+
class App {
4+
- LOGGER : Logger {static}
5+
+ App()
6+
+ main(args : String[]) {static}
7+
}
8+
class Nazgul {
9+
- name : NazgulName
10+
- nazguls : Map<NazgulName, Nazgul> {static}
11+
- Nazgul(name : NazgulName)
12+
+ getInstance(name : NazgulName) : Nazgul {static}
13+
+ getName() : NazgulName
14+
}
15+
enum NazgulEnum {
16+
+ ADUNAPHEL {static}
17+
+ AKHORAHIL {static}
18+
+ DWAR {static}
19+
+ HOARMURATH {static}
20+
+ JI_INDUR {static}
21+
+ KHAMUL {static}
22+
+ MURAZOR {static}
23+
+ REN {static}
24+
+ UVATHA {static}
25+
+ valueOf(name : String) : NazgulEnum {static}
26+
+ values() : NazgulEnum[] {static}
27+
}
28+
enum NazgulName {
29+
+ ADUNAPHEL {static}
30+
+ AKHORAHIL {static}
31+
+ DWAR {static}
32+
+ HOARMURATH {static}
33+
+ JI_INDUR {static}
34+
+ KHAMUL {static}
35+
+ MURAZOR {static}
36+
+ REN {static}
37+
+ UVATHA {static}
38+
+ valueOf(name : String) : NazgulName {static}
39+
+ values() : NazgulName[] {static}
40+
}
41+
}
42+
Nazgul --> "-name" NazgulName
43+
@enduml

multiton/bin/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
5+
6+
The MIT License
7+
Copyright © 2014-2022 Ilkka Seppälä
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
-->
28+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>java-design-patterns</artifactId>
33+
<version>1.26.0-SNAPSHOT</version>
34+
</parent>
35+
<artifactId>multiton</artifactId>
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-engine</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.junit.jupiter</groupId>
44+
<artifactId>junit-jupiter-params</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-assembly-plugin</artifactId>
53+
<executions>
54+
<execution>
55+
<configuration>
56+
<archive>
57+
<manifest>
58+
<mainClass>com.iluwatar.multiton.App</mainClass>
59+
</manifest>
60+
</archive>
61+
</configuration>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>

0 commit comments

Comments
 (0)