Answers for "dependency inversion"

0

dependency inversion

# line 6, CurrencyConverter is subclass of high-level AbatractBaseClass ABC  
# line 16, AlphaConverter is subclass of CurrencyConverter 
# line 32, 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
Posted by: Guest on February-07-2022

Python Answers by Framework

Browse Popular Code Answers by Language