Dependency Injection

Dependency injection is a design pattern commonly used in software engineering that allows for the separation of concerns and promotes code reusability. It is a technique that enables the creation of loosely coupled code, which can be easily tested, maintained, and extended. What is Dependency Injection? Dependency injection is a design pattern that allows for the separation of concerns between different parts of a program or system. In dependency injection, the dependencies required by a component or module are injected into it at runtime, rather than being hardcoded into the component or module....

April 5, 2023 · aaron

Singleton with Decorator

In Python, a singleton is a design pattern that restricts the instantiation of a class to a single object. A singleton class ensures that only one instance of the class is created and provides a global point of access to that instance. One way to implement a singleton in Python is to use a decorator. Here’s an example: def singleton(cls): instances = {} def get_instance(*args, **kwargs): if cls not in instances: instances[cls] = cls(*args, **kwargs) return instances[cls] return get_instance @singleton class MyClass: def __init__(self, x): self....

April 4, 2023 · aaron