Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dependency inversion

# DI means to depend on highLevel instead of lowLevel artifacts, basically to use a generalized facade of possible implementations
# line 7, CurrencyConverter is subclass of high-level AbatractBaseClass ABC  
# line 17, AlphaConverter is subclass of CurrencyConverter 
# line 33, App is dependent of above high-level class i.e. dependency inversion 
from abc import ABC

class CurrencyConverter(ABC):
    def convert(self, from_currency, to_currency, amount) -> float:
        pass

class FXConverter(CurrencyConverter):
    def convert(self, from_currency, to_currency, amount) -> float:
        print('Converting currency using FX API')
        print(f'{amount} {from_currency} = {amount * 1.2} {to_currency}')
        return amount * 1.15

class AlphaConverter(CurrencyConverter):
    def convert(self, from_currency, to_currency, amount) -> float:
        print('Converting currency using Alpha API')
        print(f'{amount} {from_currency} = {amount * 1.2} {to_currency}')
        return amount * 1.2

class App:
    def __init__(self, converter: CurrencyConverter):
        self.converter = converter

    def start(self):
        self.converter.convert('EUR', 'USD', 100)


if __name__ == '__main__':
    converter = AlphaConverter()
    app = App(converter)
    app.start()
# output    
# Converting currency using Alpha API
# 100 EUR = 120.0 USD

#in real life DI is similar to training a child
#to not rely on a specific elder but instead to 
#a high-level reference such as a council of elders
#that would aĺway refer the child to a specific person
#i.e. specific implementation


Comment

PREVIOUS NEXT
Code Example
Python :: binary search iterative python 
Python :: alphabetical 
Python :: python __add__ 
Python :: how to add to beginning of linked list python 
Python :: python switch 
Python :: python crosshair overlay 
Python :: find daily aggregation in pandas 
Python :: List get both index and value. 
Python :: python get previous method name 
Python :: python tabulate without index 
Python :: objects and classes in python 
Python :: stackoverflow: install old version of networkx 
Python :: matplotlib force scientific notation and define exponent 
Python :: add text to jpg python 
Python :: print(f ) python 
Python :: python square 
Python :: python using os module file name from file path 
Python :: python timedelta get days with fraction 
Python :: pandas interpolate string 
Python :: convert iso 8601 to milliseconds python 
Python :: permutation in python 
Python :: python infinite l00p 
Python :: get script text selenium python 
Python :: how to create tupple in python 
Python :: python list of dict change dicts id by position in list when moved 
Python :: compare two data frames in assert 
Python :: check if binary tree is balanced python 
Python :: label with list comprehension python 
Python :: python append 
Python :: export list to a file python 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =