Multiton - registry of singletons
http://en.wikipedia.org/wiki/Multiton_pattern
<<
>>
http://en.wikipedia.org/wiki/Multiton_pattern
<<
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
public class FooMultiton { | |
private static final Map<Object, FooMultiton> instances = new HashMap<Object, FooMultiton>(); | |
private FooMultiton() { | |
// no explicit implementation | |
} | |
public static synchronized FooMultiton getInstance(Object key) { | |
// Our "per key" singleton | |
FooMultiton instance = instances.get(key); | |
if (instance == null) { | |
// Lazily create instance | |
instance = new FooMultiton(); | |
// Add it to map | |
instances.put(key, instance); | |
} | |
return instance; | |
} | |
// other fields and methods ... | |
} |
Комментариев нет:
Отправить комментарий