So, here's the deal. I've managed to make a framework without using globals or static classes/functions.
I am using a form of dependency injection using a Factory. Since the framework will be used for various things, I'm creating a more generic Factory that will build your class, with it's dependencies recursively.
The thing is, to conserve memory, each time an object gets instantiated, the Factory stores a reference to it, so if another object has a dependency to that object, the Factory will only need to return the reference. That way we don't need to instantiate the same object twice.
This means, in lots of classes, we will have many different references to the same object. For example, if I declare Blog_model, Blog_controller, Blog_view, Form_validation to require the Config object, each of them will be instantiated with a reference to the same Config object, albeit with injection.
I'm not familiar with unit testing or any kind of automatic testing. I've just discovered that using globals and statics are bad (which is why I'm rewriting the framework I use). What I want to ask is, does this introduce a global state? Does it hinders testing in any way?
---- Update ------
It's an MVC framework written in PHP.
As far as I read this question, you have essentially created a Dependency Injection Container that only supports a single lifestyle.
Closely related to DI is the concept of lifetime management. If we ask for an instance of a specific type multiple times, do we get the same reference every time, or do we get a fresh instance each time?
If we get the same instance every time we call it the Singleton lifestyle - not to be confused with the Singleton design pattern.
If we get a new instance each time we call it the Transient lifestyle.
There are also other types of lifestyles, such as scoped, pooled, etc. but the above two are the most basic lifestyles.
It sounds to me that your DI container only supports the Singleton lifestyle. This isn't the same as Global state, but state is shared within a single instance of the container. However, if you throw away the container instance, your also throw away the shared state, so it's much easier to get rid of than global state.
Yes, it does introduce a global state, since your factory returns a reference to just created object.
You didn't say which language you are using, but if you are using c++, your factory method should return a shared_ptr (the type for the shared_ptr should be the base class of the object you are creating).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With