SingletonExtensionFactory extends DefaultExtensionFactory { @Override @SuppressWarnings("unchecked") public <T> T create(Class<T> extensionClass) { String extensionClassName = extensionClass.getName(); if (cache.containsKey(extensionClassName)) { return (T) cache.get(extensionClassName); } T extension = super.create(extensionClass); if (extensionClassNames.isEmpty() || extensionClassNames.contains(extensionClassName)) { cache.put(extensionClassName, extension); } return extension; } SingletonExtensionFactory(String... extensionClassNames); @Override @SuppressWarnings("unchecked") T create(Class<T> extensionClass);  }
@Test public void create() { ExtensionFactory extensionFactory = new SingletonExtensionFactory(); Object extensionOne = extensionFactory.create(TestExtension.class); Object extensionTwo = extensionFactory.create(TestExtension.class); assertSame(extensionOne, extensionTwo); }
@Test public void createNewEachTime() { ExtensionFactory extensionFactory = new SingletonExtensionFactory(FailTestExtension.class.getName()); Object extensionOne = extensionFactory.create(TestExtension.class); Object extensionTwo = extensionFactory.create(TestExtension.class); assertNotSame(extensionOne, extensionTwo); }