Skip to content

Commit ac57d67

Browse files
solution to issue iluwatar#602. Implemented multiton with enum, added example to app.java, a test for the enum, and commented my code
1 parent 960d2ea commit ac57d67

File tree

1 file changed

+18
-3
lines changed
  • multiton/src/main/java/com/iluwatar/multiton

1 file changed

+18
-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
}

0 commit comments

Comments
 (0)