Dependency inversion and how it could be very useful

Dependency inversion and how it could be very useful

in brief Dependency inversion means high level Modules shouldn't depend on low level modules

No alt text provided for this image

here the CoreDataController is a low level module, its easy to reuse in other projects, the problem is with high level module ConversationDataController, its tightly coupled with CoreDataController.

we cannot use the ConversationDataContoller into another app easily because it is tightly coupled with CoreDataController or in other words it has a dependecy in low level module CoreDataController.

what is loosely coupled and tightly coupled anyway ?

-Tight coupling refers to a situation where two or more software components are closely connected and depend on each other to function properly.

-Loose coupling, on the other hand, means that the components are less dependent on each other and can operate more independently

We can solve this dependency using a Database Protocol. In this way ConversationDataController can use abstract protocol without caring for type of database used, lets see how the ConversationDataController will look after applying this.

No alt text provided for this image

now you can reuse conversationDataController without hazzle into another project with implementing new class which confirms to Database Abstraction or Protocol.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了