Skip to content

Commit 4d95d38

Browse files
committed
iluwatar#590 explanation for Multiton
1 parent d2724e8 commit 4d95d38

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

multiton/README.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,88 @@ tags:
1212
Registry
1313

1414
## Intent
15-
Ensure a class only has limited number of instances, and provide a
16-
global point of access to them.
15+
Ensure a class only has limited number of instances and provide a global point of access to them.
16+
17+
## Explanation
18+
19+
Real world example
20+
21+
> The Nazgûl, also called ringwraiths or the Nine Riders, are Sauron's most terrible servants. By definition there's always nine of them.
22+
23+
In plain words
24+
25+
> Multiton pattern ensures there's predefined amount of instances available globally.
26+
27+
Wikipedia says
28+
29+
> 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.
30+
31+
**Programmatic Example**
32+
33+
Nazgul is the multiton class.
34+
35+
```java
36+
public enum NazgulName {
37+
38+
KHAMUL, MURAZOR, DWAR, JI_INDUR, AKHORAHIL, HOARMURATH, ADUNAPHEL, REN, UVATHA;
39+
}
40+
41+
public final class Nazgul {
42+
43+
private static Map<NazgulName, Nazgul> nazguls;
44+
45+
private NazgulName name;
46+
47+
static {
48+
nazguls = new ConcurrentHashMap<>();
49+
nazguls.put(NazgulName.KHAMUL, new Nazgul(NazgulName.KHAMUL));
50+
nazguls.put(NazgulName.MURAZOR, new Nazgul(NazgulName.MURAZOR));
51+
nazguls.put(NazgulName.DWAR, new Nazgul(NazgulName.DWAR));
52+
nazguls.put(NazgulName.JI_INDUR, new Nazgul(NazgulName.JI_INDUR));
53+
nazguls.put(NazgulName.AKHORAHIL, new Nazgul(NazgulName.AKHORAHIL));
54+
nazguls.put(NazgulName.HOARMURATH, new Nazgul(NazgulName.HOARMURATH));
55+
nazguls.put(NazgulName.ADUNAPHEL, new Nazgul(NazgulName.ADUNAPHEL));
56+
nazguls.put(NazgulName.REN, new Nazgul(NazgulName.REN));
57+
nazguls.put(NazgulName.UVATHA, new Nazgul(NazgulName.UVATHA));
58+
}
59+
60+
private Nazgul(NazgulName name) {
61+
this.name = name;
62+
}
63+
64+
public static Nazgul getInstance(NazgulName name) {
65+
return nazguls.get(name);
66+
}
67+
68+
public NazgulName getName() {
69+
return name;
70+
}
71+
}
72+
```
73+
74+
And here's how we access the Nazgul instances.
75+
76+
```java
77+
LOGGER.info("KHAMUL={}", Nazgul.getInstance(NazgulName.KHAMUL));
78+
LOGGER.info("MURAZOR={}", Nazgul.getInstance(NazgulName.MURAZOR));
79+
LOGGER.info("DWAR={}", Nazgul.getInstance(NazgulName.DWAR));
80+
LOGGER.info("JI_INDUR={}", Nazgul.getInstance(NazgulName.JI_INDUR));
81+
LOGGER.info("AKHORAHIL={}", Nazgul.getInstance(NazgulName.AKHORAHIL));
82+
LOGGER.info("HOARMURATH={}", Nazgul.getInstance(NazgulName.HOARMURATH));
83+
LOGGER.info("ADUNAPHEL={}", Nazgul.getInstance(NazgulName.ADUNAPHEL));
84+
LOGGER.info("REN={}", Nazgul.getInstance(NazgulName.REN));
85+
LOGGER.info("UVATHA={}", Nazgul.getInstance(NazgulName.UVATHA));
86+
87+
// KHAMUL=com.iluwatar.multiton.Nazgul@2b214b94
88+
// MURAZOR=com.iluwatar.multiton.Nazgul@17814b1c
89+
// DWAR=com.iluwatar.multiton.Nazgul@7ac9af2a
90+
// JI_INDUR=com.iluwatar.multiton.Nazgul@7bb004b8
91+
// AKHORAHIL=com.iluwatar.multiton.Nazgul@78e89bfe
92+
// HOARMURATH=com.iluwatar.multiton.Nazgul@652ce654
93+
// ADUNAPHEL=com.iluwatar.multiton.Nazgul@522ba524
94+
// REN=com.iluwatar.multiton.Nazgul@29c5ee1d
95+
// UVATHA=com.iluwatar.multiton.Nazgul@15cea7b0
96+
```
1797

1898
## Class diagram
1999
![alt text](./etc/multiton.png "Multiton")

observer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Here's the full example in action.
153153
// The hobbits are shivering in the cold weather.
154154
weather.timePasses();
155155
// The weather changed to sunny.
156-
//The sun hurts the orcs' eyes.
156+
// The sun hurts the orcs' eyes.
157157
// The happy hobbits bade in the warm sun.
158158
```
159159

0 commit comments

Comments
 (0)