There is never a single answer to the "how to do..." question in software development.
So much depends on the context and most appropriate solution often depends on the judgement and experience of the engineering team.
Abstraction and inversion of control are typical in this respect and here Keith Williams walks through some popular patterns and talks about where dependency injection fits into the jigsaw.