What is the difference between Strategy pattern and Dependency Injection?

后端 未结 9 1734
长情又很酷
长情又很酷 2020-12-04 06:13

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?

9条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 06:19

    Dude, dependency injection is a more general pattern, and it's about dependency on abstractions not concretions and it's a part of every pattern, but Strategy pattern is a solution to more specific problem

    this is the definition from wikipedia:

    DI:

    Dependency injection (DI) in object-oriented computer programming is a design pattern with a core principle of separating behavior from dependency resolution. In other words: a technique for decoupling highly dependent software components.

    Strategy Pattern:

    In computer programming, the strategy pattern (also known as the policy pattern) is a particular software design pattern, whereby algorithms can be selected at runtime.

    The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.

提交回复
热议问题