Home  /  Questions  /  Question



56   94.9
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

0   0
14 days ago
First it's important for the community to recognize that DI is a junk term. Injecting a dependency is just pointless re-terming of a simple concept: passing a reference.

On the other hand, IOC and Strategy are not the same thing. Strategy is an example of IOC. Your comment about them seeming like the same thing is interesting because Strategy is really the best example of IOC. 

In a system that uses the Strategy pattern, the top level classes are "told what to do" by the lower level, plug-in Strategy classes. Rather than a top level class branching on endless if then/statements, it just asks the Strategy to execute its particular behavior to alter the system's behavior.

It's true there may be other aspects of the system choosing which Strategy to plug-in. Or it may be the user's behavior. Either way, a different reference is passed to the container of the Strategy. We don't need to say it was "injected".

Offhand, I find it hard to think of something other than Strategy as an example of IOC. Menuing systems are often given as an IOC example. Menu's in a GUI are issuing commands, rather than some higher level part of the system asking if x menu was clicked / else if y menu was clicked, and so on. Menus can be plugged and exchanged freely. 

I find anything defined as 'pluggable' as an example of IOC. 

And one other thing: good programmers will naturally design IOC into their code, even if they don't know the term.


 1 comment
 
No, they're not the same --- Craig Colbert  5 days ago