When comparing Dependency Injection vs Template Methods, the Slant community recommends Dependency Injection for most people. In the question“What are the best Inversion of Control mechanisms? ” Dependency Injection is ranked 3rd while Template Methods is ranked 4th. The most important reason people chose Dependency Injection is:
Since the class doesn't have to know anything about the outside world, everything is more decoupled. If your class would have to fetch its own dependencies then it would have to know where to find them, and as a result, it would have to know about the larger application.
Ranked in these QuestionsQuestion Ranking
Pros
Pro More decoupled code
Since the class doesn't have to know anything about the outside world, everything is more decoupled. If your class would have to fetch its own dependencies then it would have to know where to find them, and as a result, it would have to know about the larger application.
Pro More flexible code
Since your class doesn't have to know about the concrete implementation of the dependencies, it makes them very flexible.
Cons
Con Steep Learning Curve
Significant effort is needed to grasp DI and use it correctly.