@Test public void shouldAlwaysReturnDifferentObjectsToDifferentTypes() { final String type1 = "STRING1"; final String type2 = "STRING2"; Configuration conf = new Configuration("No file"); conf.set(type1, "java.lang.String"); conf.set(type2, "java.lang.String"); SingletonsFactory<String> factory = new SingletonsFactory<String>(conf); String singleton1 = factory.getInstance(type1); String singleton2 = factory.getInstance(type2); assertTrue(singleton1 != singleton2); }
public T getInstance(String type) { if (!singletons.containsKey(type)) { synchronized (SingletonsFactory.class) { if (!singletons.containsKey(type)) { T singleton = newInstance(type); singletons.put(type, singleton); } } } return singletons.get(type); }
SingletonsFactory { public T getInstance(String type) { if (!singletons.containsKey(type)) { synchronized (SingletonsFactory.class) { if (!singletons.containsKey(type)) { T singleton = newInstance(type); singletons.put(type, singleton); } } } return singletons.get(type); } }
SingletonsFactory { public T getInstance(String type) { if (!singletons.containsKey(type)) { synchronized (SingletonsFactory.class) { if (!singletons.containsKey(type)) { T singleton = newInstance(type); singletons.put(type, singleton); } } } return singletons.get(type); } SingletonsFactory(Configuration classMap); }
SingletonsFactory { public T getInstance(String type) { if (!singletons.containsKey(type)) { synchronized (SingletonsFactory.class) { if (!singletons.containsKey(type)) { T singleton = newInstance(type); singletons.put(type, singleton); } } } return singletons.get(type); } SingletonsFactory(Configuration classMap); T getInstance(String type); }
SingletonsFactory { public T getInstance(String type) { if (!singletons.containsKey(type)) { synchronized (SingletonsFactory.class) { if (!singletons.containsKey(type)) { T singleton = newInstance(type); singletons.put(type, singleton); } } } return singletons.get(type); } SingletonsFactory(Configuration classMap); T getInstance(String type);  }