How does the SOLID open/closed principle fit in with Dependency Injection and dependency inversion

后端 未结 3 1612
夕颜
夕颜 2021-02-06 17:09

I am starting to apply SOLID principles, and am finding them slightly contradictory. My issue is as follows:

My understanding of dependency inversion principle is that

3条回答
  •  鱼传尺愫
    2021-02-06 17:23

    Modules become closed to modification once they are referenced by other modules. What becomes closed is the public API, the interface. Behavior can be changed via polymorphic substitution (implementing the interface in a new class and injecting it). Your IoC container can inject this new implementation. This ability to polymorphically substitute is the 'Open to extension' part. So, DIP and Open/Closed work together nicely.

    See Wikipedia:"During the 1990s, the open/closed principle became popularly redefined to refer to the use of abstracted interfaces..."

提交回复
热议问题