Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
You pass class dependencies as arguments to the constructor of the class that depends on them.
@highlight
Class A {
A(DependencyClass1, ...) {
this.dependency1 = DependencyClass1.new();
}
doSomethingWithDependency() {
this.dependency1.doStuff();
}
} -
nibor48773yWithin your class, there are dependencies on other classes. You can "New" these classes to get the objects within your class, but this makes code that is hard to test, brittle and tightly coupled to the dependencies.
Using Dependency Injection, you instead pass the objects directly into the class, usually in the constructor. The responsibility of instantiating these objects lies elsewhere, usually within the DI container.
One key advantage is that in tests, you can instantiate stubs or mocks of your dependencies and inject these into the class being tested.
Related Rants
I'm trying to understand Dependency Injection.
can I say dependency injection represent a class that is dependent of another class?
question
java
kotlin
dependencyinjection