Паттерн: Одиночка (Singleton)
Фрагмент: RegSingleton, api реестра [java]
  1. package singleton;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class RegSingleton {
  7. private static RegSingleton _instance;
  8. private static Map _registry = new HashMap();
  9.  
  10. protected RegSingleton() { }
  11.  
  12. public static void Register(String name, RegSingleton obj) {
  13. _registry.put(name, obj);
  14. }
  15.  
  16. public static RegSingleton Instance() {
  17. if (_instance == null) {
  18. // User define this name in system property file
  19. // before program starts
  20. String name = System.getProperty("patterns.book.singletonName");
  21. _instance = RegSingleton.Lookup(name);
  22. }
  23. return _instance;
  24. }
  25.  
  26. protected static RegSingleton Lookup(String name) {
  27. return (RegSingleton) _registry.get(name);
  28. }
  29. }