Skip to content

Commit 477da92

Browse files
authored
Merge pull request iluwatar#724 from anthonycampbell/master
My solution to issue iluwatar#602.
2 parents c8fd0bc + ac57d67 commit 477da92

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

multiton/src/main/java/com/iluwatar/multiton/App.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
* pattern defines many globally accessible objects. The client asks for the correct instance from
3232
* the Multiton by passing an enumeration as parameter.
3333
* <p>
34-
* In this example {@link Nazgul} is the Multiton and we can ask single {@link Nazgul} from it using
35-
* {@link NazgulName}. The {@link Nazgul}s are statically initialized and stored in concurrent hash
36-
* map.
34+
* There is more than one way to implement the multiton design pattern. In the first example
35+
* {@link Nazgul} is the Multiton and we can ask single {@link Nazgul} from it using {@link NazgulName}.
36+
* The {@link Nazgul}s are statically initialized and stored in concurrent hash map.
37+
* <p>
38+
* In the enum implementation {@link NazgulEnum} is the multiton. It is static and mutable because
39+
* of the way java supports enums.
3740
*
3841
*/
3942
public class App {
@@ -46,6 +49,7 @@ public class App {
4649
* @param args command line args
4750
*/
4851
public static void main(String[] args) {
52+
// eagerly initialized multiton
4953
LOGGER.info("KHAMUL={}", Nazgul.getInstance(NazgulName.KHAMUL));
5054
LOGGER.info("MURAZOR={}", Nazgul.getInstance(NazgulName.MURAZOR));
5155
LOGGER.info("DWAR={}", Nazgul.getInstance(NazgulName.DWAR));
@@ -55,5 +59,16 @@ public static void main(String[] args) {
5559
LOGGER.info("ADUNAPHEL={}", Nazgul.getInstance(NazgulName.ADUNAPHEL));
5660
LOGGER.info("REN={}", Nazgul.getInstance(NazgulName.REN));
5761
LOGGER.info("UVATHA={}", Nazgul.getInstance(NazgulName.UVATHA));
62+
63+
// enum multiton
64+
LOGGER.info("KHAMUL={}", NazgulEnum.KHAMUL);
65+
LOGGER.info("MURAZOR={}", NazgulEnum.MURAZOR);
66+
LOGGER.info("DWAR={}", NazgulEnum.DWAR);
67+
LOGGER.info("JI_INDUR={}", NazgulEnum.JI_INDUR);
68+
LOGGER.info("AKHORAHIL={}", NazgulEnum.AKHORAHIL);
69+
LOGGER.info("HOARMURATH={}", NazgulEnum.HOARMURATH);
70+
LOGGER.info("ADUNAPHEL={}", NazgulEnum.ADUNAPHEL);
71+
LOGGER.info("REN={}", NazgulEnum.REN);
72+
LOGGER.info("UVATHA={}", NazgulEnum.UVATHA);
5873
}
5974
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.multiton;
24+
25+
/**
26+
* enum based multiton implementation
27+
*
28+
*/
29+
public enum NazgulEnum {
30+
31+
KHAMUL, MURAZOR, DWAR, JI_INDUR, AKHORAHIL, HOARMURATH, ADUNAPHEL, REN, UVATHA;
32+
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.multiton;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
import org.junit.jupiter.api.Test;
28+
29+
/**
30+
* @author anthony
31+
*
32+
*/
33+
class NazgulEnumTest {
34+
35+
/**
36+
* Check that multiple calls to any one of the instances in the multiton returns
37+
* only that one particular instance, and do that for all instances in multiton
38+
*/
39+
@Test
40+
public void testTheSameObjectIsReturnedWithMultipleCalls() {
41+
for (int i = 0; i < NazgulEnum.values().length; i++) {
42+
NazgulEnum instance1 = NazgulEnum.values()[i];
43+
NazgulEnum instance2 = NazgulEnum.values()[i];
44+
NazgulEnum instance3 = NazgulEnum.values()[i];
45+
assertSame(instance1, instance2);
46+
assertSame(instance1, instance3);
47+
assertSame(instance2, instance3);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)