inversi ketergantungan
# 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
ruperto2770