

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Strategy pattern and Dependency Injection both allow us to set / inject objects at run time. What is the difference between Strategy pattern and Dependency Injection?

share|improve this question
Possible dup:… – mattumotu Feb 17 '15 at 10:29
Strategy pattern might use Dependency Injection – TechWisdom Jun 21 '15 at 22:20
up vote 54 down vote accepted

DI and Strategy work in the same way, but Strategy is used for more fine-grained and short-lived dependencies.

When an object is configured with a "fixed" Strategy, for example when the object is constructed, the distinction between Strategy and DI blurs. But in a DI scenario it is more unusual that the dependencies of objects change during their lifetimes, while this is not uncommon with Strategy.

Also, you can pass strategies as arguments to methods, while the related concept of method argument injection is not widespread and mostly used in the context of automated testing only.

Strategy focuses on intent and encourages you to create an interface with different implementations that obey the same behavioral contract. DI is more about just having an implementation of some behavior and providing it.

With DI you can decompose your program for other reasons than just to be able to swap parts of the implementation. An interface used in DI with only one implementation is very common. A "Strategy" with only one concrete implementation (ever) is not a real problem but is probably closer to DI.

share|improve this answer
Good explanation... – Nero Nov 14 '10 at 9:13
An interface used in DI with only one implementation is very common - so then what is DI in this particular case? – Kalpesh Soni Sep 26 '13 at 16:06
This quote basically explains it all: in a DI scenario it is more unusual that the dependencies of objects change during their lifetimes, while this is not uncommon with Strategy – Sergey Telshevsky May 22 '14 at 8:36

Dude, dependency injection is a more general pattern, and it's about dependency on abstractions not concretions and it's a part of every pattern, but Strategy pattern is a solution to more specific problem

this is the definition from wikipedia:


Dependency injection (DI) in object-oriented computer programming is a design pattern with a core principle of separating behavior from dependency resolution. In other words: a technique for decoupling highly dependent software components.

Strategy Pattern:



share|improve this answer
+1 for beginning with "dude". – MEMark Dec 3 '13 at 14:40

The difference is what they are trying to achieve. The Strategy pattern is used in situations where you know that you want to swap out implementations. As an example, you might want to format data in different ways - you could use the strategy pattern to swap out an XML formatter or CSV formatter, etc.

Dependency Injection is different in that the user is not trying to change the runtime behaviour. Following the example above, we might be creating an XML export program that uses an XML formatter. Rather than structuring the code like this:

public class DataExporter() {
  XMLFormatter formatter = new XMLFormatter();

you would 'inject' the formatter in the constructor:

public class DataExporter {
  IFormatter formatter = null;

  public DataExporter(IDataFormatter dataFormatter) {
    this.formatter = dataFormatter;

DataExporter exporter = new DataExporter(new XMLFormatter());

There are a few justifications for Dependency Injection, but the primary one is for testing. You might have a case where you have a persistence engine of some sort (such as a database). However, it can be a pain to use a real database when you're running tests repeatedly. So, for your test cases, you would inject a dummy database, so that you don't incur that overhead.

Using this example, you can see the difference: we always plan on using a data storage strategy, and it's the one that we pass in (the real DB instance). However, in development and testing, we want to use different dependencies, so we inject different concretions.

share|improve this answer
Clearly understood with real time example... – Nero Nov 14 '10 at 11:07

Can I reprase the question by replacing 'difference' with 'relationship' between Dependency Injection and Strategy? Strategy is an arena to use your dependency injection skills. Real ways to implement dependency injection are as follows:-

  1. Events
  2. Configuration files of unity/structure map(or programmatically) etc.
  3. Extension Methods
  4. Abstract Factory pattern
  5. Inversion of control pattern(used by both strategy and Abstract Factory)

There is one thing though that makes strategy stands apart. As you know in Unity when the application starts up all dependencies are set and we can't change it further. But strategy supports runtime dependency change. But WE have to manage the dependency not the resposibility of Strategy Pattern.

Actually strategy does not talk about dependency injection. If needed it can be done through Abstract Factory inside a Strategy pattern. Strategy only talks about creating a family of classes with interface and 'playing' with it. While playing, if we find the classes are in a different tier then we have to inject it ourselves but not the job of 'strategy'.

share|improve this answer

Your Answer


