Home  /  Questions  /  Question



56   94.6
Mar 23, 2011


Difference between Strategy and Inversion of Control (IOC)?

Hi,

When getting into Design Patterns I first studied the GoF patterns, which includes Strategy.  After that I started tackling ASP.NET MVC in which the Inversion of Control (IOC) pattern is frequently used (IOC is also called Dependency Injection or DI), I clearly see how this benefits loose coupling, configuration without recompile, testing, etc.

My question is this. I don't see any difference between Strategy and IOC. From what I can tell they are exactly the same.
So why did they 're-invent' a different name?




1,128   99.9
Mar 23, 2011
The way I see it is like this...

Short answer: No, they're not the same.

Slightly longer answer: No, they're not the same. But the Strategy pattern works well with IoC/DI.

Long answer: The strategy pattern is useful when you want classes to depend on the interface rather than the implementation. And you can easily swap out behavior depending on which concrete implementation you provide.

IoC/DI comes into play when you want the concrete strategy implementation injected into a class. For example, you could use the DI Framework Ninject and configure it so that it will know which concrete strategy implementation to inject into a class in specific scenarios.

Hope that helps!



Regarding IoC == DI, I have seen several posts on the topic claiming they're the same OR that DI is a type of IoC. Where IoC is a broader principle that covers more than DI. Haven't read enough to decide what is what though. I believe more people are leaning towards the latter.
 4 comments
 
Thanks Robert. So, if Strategy is just one of the ways that IOC is implemented, what other approaches are there? The only way I have seen it done is using the Strategy Pattern. What approaches does Ninject offer? Thanks a lot! --- John Telstra  Mar 23, 2011
 
Agreed, Strategy is commonly used and one I have used a lot myself. I guess another approach could be to use an Event Driven architecture which I find more and more intriguing. You can read more about it here http://elegantcode.com/2010/01/06/event-driven-architecture-publishing-events-using-an-ioc-container. --- Robert Blixt  Mar 23, 2011
 
Good answer Robert. --- Rajesh Pillai  Mar 24, 2011
 
Thanks :) --- Robert Blixt  Mar 24, 2011