Skip to content

Standard Factory Manager

Marc Hermans edited this page Aug 12, 2017 · 2 revisions

Standard Factory Controller (SFC)

Coding Patterns in use.

Inversion of Control (IoC)

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

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 implementation in Minecolonies

Standard Factory Controller [SFC] (IoC-Container)

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 Interface (Factory)

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.

Why both a Factory and a IoC-Controller

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.

How to ...

... make a new Factory.

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.

... register a newly created Factory to the SFC

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>.

... create a new Factory that takes NO input

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.

... create a new Instance of type: 'Type'

Type instance = StandardFactoryController.getInstance().getNewInstance(inputType, new TypeToken<Type> {}, additionalParam1, additionalParam2);

... create a new Instance of type: 'NoArgs' that takes no Input.

NoArgs instance = StandardFactoryController.getInstance().getNewInstance(new TypeToken<NoArgs> {});

... serialize a Object

NBTTagCompound nbt = StandardFactoryController.getInstance().serialize(object);

... deserialize a Object of type: 'Deser'

Deser deserialized = StandardFactoryController.getInstance().deserialize(nbt);

Examples for IFactory implementations

The following classes are examples of implementations of the IFactory class:

  • StandardTokenFactory
  • StaticLocation.Factory
  • EntityLocation.Factory
  • StandardRequestFactories.ItemStackFactory
  • StandardRequestFactories.DeliveryFactory