-
Notifications
You must be signed in to change notification settings - Fork 1
Factories
Factories are groupings of generators that produce the same type of objects. They are commonly used to decorate the functionalities of its inner generators. All factories have a default generator, and generate() calls that do not specify the key will be delegated to the default generator. Factories are also themselves generators, so you can easily introduce them where an equivalent output generator would fit.
Just like for generators, a base implementation is provided that future-proof your factories from any potential changes: the BaseFactory. If you extend the BaseFactory you only need to implement the decorate(T, Modifier[]) method that is called after having generated and applied modifiers to the new object.
A recording factory keeps an accessible record of everything it has created. This is very helpful when integration testing with databases. The class also maintains a pool of all existing recording factories to make sure that you can always find back your records.
Map<String, Generator<City>> generators = new HashMap<>();
generators.put("default", new CityGenerator());
generators.put("capital", new CityGenerator.CapitalGenerator());
Factory<String, City> factory = new RecordingFactory<>(City.class, "default", generators);Usage:
// Make one item with the default generator
factory.generate();
// Make one item with the capitals generator
factory.generate("capital");
// Make 5 items with the capitals generator
factory.generate("capital", 5);