This commit is contained in:
Roman Mogylatov 2020-09-03 23:10:36 -04:00
parent 14c2ecae8f
commit 74df2ed00d
2 changed files with 8 additions and 8 deletions

View File

@ -70,7 +70,7 @@ Coupling and cohesion are about how tough the components are tied.
When the cohesion is high the coupling is low. When the cohesion is high the coupling is low.
High cohesion brings the flexibility. Your code becomes easier to change and to test. High cohesion brings the flexibility. Your code becomes easier to change and test.
How to implement dependency injection? How to implement dependency injection?
-------------------------------------- --------------------------------------
@ -87,14 +87,14 @@ Before:
class ApiClient: class ApiClient:
def __init__(self): def __init__(self):
self.api_key = os.getenv('API_KEY') self.api_key = os.getenv('API_KEY') # <-- the dependency
self.timeout = os.getenv('TIMEOUT') self.timeout = os.getenv('TIMEOUT') # <-- the dependency
class Service: class Service:
def __init__(self): def __init__(self):
self.api_client = ApiClient() self.api_client = ApiClient() # <-- the dependency
if __name__ == '__main__': if __name__ == '__main__':
@ -111,14 +111,14 @@ After:
class ApiClient: class ApiClient:
def __init__(self, api_key: str, timeout: int): def __init__(self, api_key: str, timeout: int):
self.api_key = api_key self.api_key = api_key # <-- the dependency is injected
self.timeout = timeout self.timeout = timeout # <-- the dependency is injected
class Service: class Service:
def __init__(self, api_client: ApiClient): def __init__(self, api_client: ApiClient):
self.api_client = api_client self.api_client = api_client # <-- the dependency is injected
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -21,7 +21,7 @@ Coupling and cohesion are about how tough the components are tied.
When the cohesion is high the coupling is low. When the cohesion is high the coupling is low.
High cohesion brings the flexibility. Your code becomes easier to change and to test. High cohesion brings the flexibility. Your code becomes easier to change and test.
The example The example
~~~~~~~~~~~ ~~~~~~~~~~~