Dependency Inversion Principle (SOLID) vs Encapsulation (Pillars of OOP)

后端 未结 7 630
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 02:35

I was recently having a debate about the Dependency Inversion Principle, Inversion of Control and Dependency Injection. In relation to this topic we w

7条回答
  •  既然无缘
    2021-01-30 02:50

    Encapsulation does not contradict with Dependency Inversion Principles in Object-Oriented Programming world. For example in a car design, you will have an 'internal engine' which will be encapsulated from outside world, and also 'wheels' that can be replaced easily, and considered as outside component of the car. The car has specification (interface) to rotate the shaft of the wheels, and the wheels component implements part that interact with the shaft.

    Here, The internal engine represents the encapsulation process, while the wheel components represent the Dependency Inversion Principles (DIP) in the car design. With DIP, basically we prevent building a monolithic object, and instead we make our object composable. Can you image you build a car, where you cannot replace the wheels because they are built-in into the car.

    Also you can read more about Dependency Inversion Principles in more details in my blog Here.

提交回复
热议问题