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?
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.