In my post I wrote about IoC-containers, how to write them and when to use them and which alternatives do we have.
The first point of this article was that we don’t have good alternatives for factories now. I have tried to replace IoC-factory with a strategy pattern, but this works only for small count of items that should be stored in the factory.
The second point was that normal “Delphi” method (see example below) that is often used in applications is not so flexible and testable and hides dependencies. I consider to use it only when I want to have a factory which lifetime is equal to the lifetime of the application.
How to register a class in this factory:
Example of usage:
Thrid point was about factories that use RTTI. I think that this is the best method to write factories. I found following advantages:
- We can create and free such a factory any time
- We can use one factory, but fill it with different classes in any point of time
- We have a clear context of application/library/software component and don’t need to make our factory global
There is a problem with RTTI in Delphi, but to create such a factory is possible and doesn’t take so much time. I also wrote an example application :
This type of factories I want to use everywhere, because of its flexibility. I would also recommend to create a config files and store dependencies between them or use a framework with such a container.
That’s all :)
Here are some links that helped me to understand IoC, dependency injection and singletones: