15

How is strategy pattern is different then dependency injection?

ie below is what you can do with Strategy pattern:

class Foo{
   private readonly ISortAlgo _sortAlgo;

  public Foo(ISortAlgo sortAlgo)
  {
     _sortAlgo = sortAlgo;
  }

  public void Sort()
  {
    _sortAlgo.sort();
  }

}

with DI you can do the same, essentially you can have constructor, setter, interface etc. injection. and it would give the same effect as Strategy pattern. I am aware that DI is also set of other principles, such as loose coupling, testability, wiring etc.

In terms of implementation i dont see much difference.

what s the difference between strategy pattern and DI?

DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • The current answers are not especially satisfying. Better answers would focus pragmatically on the differences in which the variable behaviors are injected at run time. – Mario Aug 19 '14 at 15:09

4 Answers4

15

First, dependency injection has not only constructor injection as method to inject, but also property, method injection and ambient context.

Second, stategy defines behaviour, so client could select special one that matches on his needs. While dependency injection works with abstraction of external dependencies.

Steven
  • 166,672
  • 24
  • 332
  • 435
Akim
  • 8,469
  • 2
  • 31
  • 49
  • 1
    +1 for a nice summary. I think that some confusion is due to the fact that the two DP's use the same mechanisms but have different scope and intent. – Anders Johansen Aug 08 '12 at 06:51
13

The Strategy pattern allows an object's behavior (i.e. its algorithms) to be selected at runtime, where as Dependency injection allows the removal of hard-coded dependencies.

They are therefore not competitors. Their implementations might be similar, their aim, however, is different.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 1
    "passed to a method, but this would not be considered to be DI". Correction: DI comes in multiple forms. Constructor is the most common form (and often the best option), but method injection is also a form of Dependency Injection. – Steven Aug 10 '12 at 14:19
  • Would you also consider it to be DI if it was passed to a method just to perform some action (the case I had in mind) and not to initialize a class? – Olivier Jacot-Descombes Aug 10 '12 at 14:34
  • The dependency is injected into the method, so Yes, it is a form of dependency injection. However, method injection is usually a very fragile way of injecting dependencies, since the dependency has to be part of the contract, and it will be cumbersome to pass dependencies from method to method to method to method down the call stack. – Steven Aug 10 '12 at 15:57
  • 1
    Instead of saying Algorithm's behavior, it will be good to call Object's behavior, will it be? Because Algorithm itself is a behavior. – Aditya Bokade Nov 24 '14 at 09:26
  • Yes, I improved my text. – Olivier Jacot-Descombes Nov 24 '14 at 14:42
6

Strategy allows you to change the behaviour of an object. DI is a design pattern that allows you to be dependent on abstractions.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
user854301
  • 5,383
  • 3
  • 28
  • 37
4

Dependency Injection is a pattern that help you split construction form logic which is great for testing and system extensibility. It can also be used in place where other patters fit e.g. Singleton.

Strategy pattern solves a different problem. It lets runtime to choose the algorithm - in OOP through polymorphism.

Surely, they can work together.

Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108