Creator - examples
The default context can be set on updates like this:
FactoryContext.getInstance().setDefaultContext(context, Domain1Creator.class);
For convenience, we can pass more than one creator type to that method.
We still have to define on which exact criteria, means context state, a certain factory shall be choosen by the framework. So we need to assign to each factory at least one concrete context state or vice versa we assign exactly one factory to each separate and relevant context state. Normally we need to do this just once without updates.
The ConcreteFactory1 shall be choosen if the following state is given:
CreationContext<Domain1Factory> context = new GenericCreationContext<>(
"PROD",
"BAR1",
null,
new Month() {
@override
public boolean equals(Object obj) {
// true if obj has value 3-8
}
}
);
Note: this context state shall match for certain months only. it's a summer product
We can initialize the factory and assign it to that context like this:
FactoryContext.getInstance().addContext(context, new ConcreteFactory1());
We do the same for the ConcreteFactory2:
CreationContext<Domain1Factory> context = new GenericCreationContext<>(
"PROD",
"BAR2",
null,
null,
);
FactoryContext.getInstance().addContext(context, new ConcreteFactory2());
Alternatively we could do this for one context / factory together with the setting of the default context in one step:
FactoryContext.getInstance().addDefaultContext(context, new ConcreteFactory1(), Domain1Creator.class);
If nothing important has been forgotton, then the object creation should work now as specified initially!
Foobar foobar = new FoobarCreator().create();
Páginas |