FactoryCreator { public F create(FactoryContext<R> context) { F result; if (scope==Scope.PROTOTYPE){ result = creator.apply(context); } else { if (factory==null){ factory=creator.apply(context); } result=factory; } templateId.serializeTo(result); return result; } FactoryCreator(FactoryTemplateId<F> templateId, Scope scope, Function<FactoryContext<R>, F> creator); @Override String toString(); boolean match(Class<?> clazzMatch,String name); boolean match(Class<?> clazzMatch); boolean matchLiveObjectClass(Class<?> liveObjectCLass); boolean isDuplicate(FactoryCreator<?,R> factoryCreator); F create(FactoryContext<R> context); Scope getScope(); boolean isEmpty(); @SuppressWarnings("unchecked") void fillFromExistingFactoryTree(Map<FactoryCreatorIdentifier, FactoryBase<?,?>> classToFactory); F createNew(FactoryContext<R> context); void reset(); FactoryCreator<F,R> copy();  }
@Test public void test_singleton_scope(){ FactoryCreator<ExampleFactoryA,ExampleFactoryA> factoryCreator = new FactoryCreator<>(new FactoryTemplateId<>(ExampleFactoryA.class, ""), Scope.SINGLETON, context -> { return new ExampleFactoryA(); }); ExampleFactoryA first = factoryCreator.create(null); ExampleFactoryA second = factoryCreator.create(null); Assertions.assertEquals(first,second); }
@Test public void test_prototype_scope(){ FactoryCreator<ExampleFactoryA,ExampleFactoryA> factoryCreator = new FactoryCreator<>(new FactoryTemplateId<>(ExampleFactoryA.class, ""), Scope.PROTOTYPE, context -> { return new ExampleFactoryA(); }); ExampleFactoryA first = factoryCreator.create(null); ExampleFactoryA second = factoryCreator.create(null); Assertions.assertNotEquals(first,second); }