I've been hearing about using the dependency injection over Singleton for my colleague. I still can't make out if it they are two orthogonal patterns which can be replaced with one another? Or is DI a method to make the Singleton pattern testable?
Please take a look at the following code snippet.
IMathFace obj = Singleton.Instance;
SingletonConsumer singConsumer = new SingletonConsumer(obj);
singConsumer.ConsumerAdd(10,20);
The SingletonConsumer
is accepting a parameter of type IMathFace
. Instead of accessing the singleton class internally, SingletonConsumer
will get the singleton instance passed by the caller. Is this a good example of consuming singleton class via dependency injection?