🧰 Developer’s toolbox: 💉 Dependency Injection Design Pattern
Instead of creating the dependency internally an object can receive it from the outside.
In software engineering, dependency injection is a technique whereby one object supplies the dependencies of another object. A “dependency” is an object that can be used, for example as a service. Instead of a client specifying which service it will use, something tells the client what service to use. The “injection” refers to the passing of a dependency (a service) into the object (a client) that would use it. The service is made part of the client’s state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.
The intent behind dependency injection is to achieve Separation of Concerns of construction and use of objects. This can increase readability and code reuse.
Github repo: link.
Example
Consider the case of of creation of a car with different engines.
Swift
TypeScript
Output
P.S. We would be happy to see comments according to mistakes & typos.