Provider
- Overview
- API
- Example
Provider base class allows you to create simple IOC containers. Ideally your provider should have virtuals methods which return interface types that can be overriding in concrete providers (runtime provider & testing provider). Main goal of this pattern is to decouple dependencies and easy testing.
Implementation#
Singletons
Can be created by storing the objects in member variables
Initialization#
Providers need to be initialized once before use and this can be done by calling the initialize method.
Initialize has 2 overloads :-
Initialize(provider, initializer):-- provider - it's the concrete instance of your provider, usefull when you are overiding your provider and want to use differnt provider depending upon config
- initializer - it's a callback that is called just after the provider is initialized, you can set singleton member variables here for eager initialization
Initialize(initializer)- same as above just that provider is created according to the generic type, can't use different implementation's of provider in this case
Usage#
Objects can be requested in the following manner.
Provider pattern used in a voxel engine
Below example shows how a provider is being used in a voxel terrain generation system.#
This base class is extended by 2 classes :-#
ColoredVoxelProvider- used at runtime for colored voxel terrain generation systemTestVoxelProvider- used for testing of the voxel terrain generation system
Finally the provider is initialized in the system.#
The Provider() method is overwritten depending upon config and the correct provider is initialized.