Skip to content

Instantly share code, notes, and snippets.

@MykolaBova
Created September 16, 2014 15:19
Show Gist options
  • Save MykolaBova/0ff3be9ffae2f7e31964 to your computer and use it in GitHub Desktop.
Save MykolaBova/0ff3be9ffae2f7e31964 to your computer and use it in GitHub Desktop.
FooMultiton
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 ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment