Imagine you have a class which gets XML data, parses it and then stores the parsed data on a database. You could very well write it like this:
1 2 3 4 5 6
There is (at least) one big problem with this approach, and it will manifest itself when you want to support another data format, such as JSON. The Consumer class is tightly coupled with the XMLParser, and therefore it’s hard to add other parsers, so we need to decouple them. We’re going to do that using a technique called Dependency Injection, like so:
1 2 3 4 5 6 7 8 9 10 11 12
This way we can inject the parser on initialization, or even after that. Note that we have a default which is the XMLParser, but it is not mandatory to have a default value.
So, after the refactoring, adding a JSONParser is easy.
This technique can be used in many scenarios, in this example we used it as part of a pattern that’s called strategy pattern.