-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multiton.java
45 lines (37 loc) · 1.15 KB
/
Multiton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Multiton
package creational.singleton;
import java.util.HashMap;
enum Subsystem
{
PRIMARY,
AUXILIARY,
FALLBACK
}
// Multiton: an object that exists in a finite set of subsystems
class Printer
{
private static int instanceCount = 0;
private Printer(){
instanceCount++;
System.out.println("A total of " +
instanceCount + " instances created so far.");
}
private static HashMap<Subsystem, Printer>
instances = new HashMap<>();
// Provide a subsystem, you get a singleton for that subsystem
public static Printer get(Subsystem subsystem)
{
if (instances.containsKey(subsystem))
return instances.get(subsystem);
Printer instance = new Printer(); // construction/lazy loading is put in the hash map!
instances.put(subsystem, instance);
return instance;
}
}
class MultitonDemo {
public static void main(String[] args) {
Printer main = Printer.get(Subsystem.PRIMARY); // constructor is in the hashmap
Printer aux = Printer.get(Subsystem.AUXILIARY);
Printer aux2 = Printer.get(Subsystem.AUXILIARY); // not duplicated
}
}