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?
Dependency Injection is more of a architectural pattern for loosely coupling software components. Factory pattern is just one way to separate the responsibility of creating objects of other classes to another entity. Factory pattern can be called as a tool to implement DI.
In-State pattern, an individual state can contain the reference of Context, to implement state transitions. But Strategies doesn't contain the reference of Context, where they are used. State encapsulate state of an Object. While Strategy Pattern encapsulates an algorithm or strategy.
The factory pattern is a creational pattern while the strategy is a behavioral pattern which means they were created using different approaches. We can solve problems in real life using both approaches but before using them we can check which approach is simpler and more effective to solve our problem.
There are three types of dependency injection — constructor injection, method injection, and property injection.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With