-
-
Notifications
You must be signed in to change notification settings - Fork 347
Standard Factory Manager
Inversion of Control is a coding pattern in which objects of specific types are not created by calling the types constructor and passing it the requested Parameters, but by calling an IoC-Containers creation method and giving it the parameters and the type in question.
The container in question will then create the object of the requested type (or an object of a subtype of the requested type, e.a.: a HashMap when a Map is requested) and return it to the caller of its creation method.
This method makes it possible that the requester can create objects without having to know how the object in question is created.
The factory system is a coding pattern that allows for the creation on Objects in a specific way. In this case objects are not created by calling the constructor but requesting a new instance of the object at the factory.
A factory can only construct a single type, but as with constructors it can take more then one set of arguments to do so.
This makes it possible to construct a single type in a bunch of different ways without having to call the constructor of the object.
The SFC is an implementation of the IoC pattern. It serves as a central point to construct objects of a specific type. It also allows a user to serialize and deserialize the objects it creates to delegate this functionality away from the user.
The IFactory<Input, Output>
functions as the Factory for specific input and outputs.
The input is in this case the primary input used during construction of the output. Additional parameters are accepted by the method that is called to construct a new instance of the output type.
The factory also has methods to handle serialization and deserialization of the output type. The serialization is an additional method, however the deserialization can be seen as a special factory method which takes an NBTTagCompound as a input type.
The advantage of using an IoC-Controller is that it can function as an abstraction layer between between the implementation of the IFactory<Input, Output>
interface and the requestor. This is especially usefull in cases were a Interface is requested as type (e.a.: IToken, or IRequest) as this allows for switching out the actuall implementation as well as having a single point to contact when a new instance of a type is needed, without having to know which factory is needed to construct a given type.
It is simple really implement the methods of the IFactory<Input, Ouput> Interface.
If you need to (de)serialize other objects during the (de)serialization process, you are given an instance of an IFactoryController
which is the Interface that the SFC implements, you can call its methods to serialize and deserialize objects as long as they are known to the controller.
The class StandardFactoryControllerInitializer
handles the initialization of the SFC in its onPreInit()
method.
In it make a call to StandardFactoryController.getInstance().registerNewFactory(factory)
, where factory
is a instance of your new IFactory<Input, Output>.
Again implement the IFactory<Input, Output>
Interface, but this time use the FactoryVoidInput<Output>
as the input type. The SFC will then know that no input for this factory is required and will substitute in when a new instance of your type is requested.
Type instance = StandardFactoryController.getInstance().getNewInstance(inputType, new TypeToken<Type> {}, additionalParam1, additionalParam2);
NoArgs instance = StandardFactoryController.getInstance().getNewInstance(new TypeToken<NoArgs> {});
NBTTagCompound nbt = StandardFactoryController.getInstance().serialize(object);
Deser deserialized = StandardFactoryController.getInstance().deserialize(nbt);
The following classes are examples of implementations of the IFactory class:
StandardTokenFactory
StaticLocation.Factory
EntityLocation.Factory
StandardRequestFactories.ItemStackFactory
StandardRequestFactories.DeliveryFactory