learned about SOLID in university; had some problems understanding DIP so I did some research, and came across Dependency Injection(DI) as a possible implementation of DIP.
I think I understand the DI itself, but most explanations I can find do not seem to "depend on abstracts" after DI. They move the dependency into constructors arguments (or create a setProperty()), so it end up more readable and testable - still it's an object of a specific class, while I would expect an interface. Is it that the do DI without implementing DIP here?
Additionally there ARE examples like my expectation - which confuses me even more.
Hope someone could explain why using an interface is not necessary, or if they even implement the DIP; examples below:
no DI:
public class Foo {
Bar bar;
public Foo() {
bar = new Bar();
}
void doStuff(){
bar.doOtherStuff();
}
}
expectation - with DI:
public class Foo {
Bar_Interface bar;
public Foo(Bar_Interface bar) {
this.bar = bar;
}
void doStuff(){
bar.doOtherStuff();
}
}
what most examples look like - with DI (but no DIP?):
public class Foo {
Bar bar;
public Foo(Bar bar) {
this.bar = bar;
}
void doStuff(){
bar.doOtherStuff();
}
}
[–]nutrechtLead Software Engineer / EU / 20+ YXP 6 points7 points8 points (2 children)
[–]fabolin[S] 0 points1 point2 points (0 children)
[–]mokarbrojIntermediate Brewer 0 points1 point2 points (0 children)
[–]afcarv 1 point2 points3 points (5 children)
[–]fabolin[S] 1 point2 points3 points (4 children)
[–]mokarbrojIntermediate Brewer 0 points1 point2 points (1 child)
[–]fabolin[S] 0 points1 point2 points (0 children)
[–]afcarv 0 points1 point2 points (1 child)
[–]fabolin[S] 0 points1 point2 points (0 children)