Dependency Inversion Principle
Dependency Inversion Principle: High-level modules should not depend on low-level modules. Both should depend on abstractions. Here is an example of a high-level module that violates the Dependency Inversion Principle by depending on a low-level module:
import lowlevel_module
class HighLevelModule:
def do_something(self):
lowlevel_module.do_something_lowlevel()
To adhere to the Dependency Inversion Principle, we can introduce an abstraction between the high-level and low-level modules:
class LowLevelModule:
def do_something_lowlevel(self):
pass
class HighLevelModule:
def __init__(self, lowlevel_module):
self.lowlevel_module = lowlevel_module
def do_something(self):
self.lowlevel_module.do_something_lowlevel()
lowlevel_module = LowLevelModule()
highlevel_module = HighLevelModule(lowlevel_module)