-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependecy_injection.py
More file actions
32 lines (26 loc) · 1.26 KB
/
dependecy_injection.py
File metadata and controls
32 lines (26 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Dependency Injection is a software design pattern that allows the application components to depend on abstractions instead of concrete implementations. The goal of Dependency Injection is to decouple the components of an application, making them more reusable, testable, and maintainable. Decorators can be used to implement Dependency Injection in Python.
# Here's an example of how to use a decorator to implement Dependency Injection:
# Define a decorator that injects a dependency
def inject_dependency(dependency):
def decorator(func):
def wrapper(*args, **kwargs):
# Inject the dependency into the function's arguments
return func(dependency, *args, **kwargs)
return wrapper
return decorator
# Define a class that represents a dependency
class MyDependency:
def some_method(self):
return "Hello, Sreekanth!"
# Define a class that represents a service
@inject_dependency(MyDependency())
class MyService:
def __init__(self, dependency):
self.dependency = dependency
def do_something(self):
# Use the injected dependency
result = self.dependency.some_method()
print(result)
# Create an instance of the service and call the method
service = MyService()
service.do_something()