I'm working on a large codebase that has made extensive use of the Singleton pattern, as well as some Globals. I've just started to try and write some unit tests, but Singletons and Globals are causing me lots of problems, and after reading up, Dependency Injection would seem the way to go.
The refactoring task ahead to make this change is very daunting, and I'm trying to work out the best approach. As far as I understand the basic idea is to take something like this:
foo()
{
GraphicsCache::Instance()->GetMyImage();
// do stuff
}
and turn it into something like this:
foo(GraphicsCache *Cache)
{
Cache->GetMyImage();
// do stuff
}
That way I can make mocks of these objects and use the mocks in my tests. But there are lots of these types of objects (event loggers, network objects, other caches etc.), which pretty much means I'm going to end up passing a lot of objects around all over the place, and will end up adding a lot of code all over the place. Have I got the right idea? Is there a better way to do this?
One idea I had was to have a single object that is a container of all of these currently global objects, and all I have to do is pass around a single reference, but that doesn't feel right as most places won't need access to every single global object, just a sub set.