3 ms·
DI has nothing to do with the differences between classes/objects and functions/closures, DI and IoC are about reusable code and being able to switch out implem
by vips7L 6y ago
DI has nothing to do with the differences between classes/objects and functions/closures, DI and IoC are about reusable code and being able to switch out implementations. You can do IoC in either pattern.
In Java functions/closures and objects/classes are the same thing. You have the best of both worlds. You can for instance define a function that takes an argument a and a function b and pass it either a functional lambda or a class that implements said function:
static void doSomething(int a, Runnable b) {
if (a > 0) {
b.run();
}
}
doSomething(1, () -> System.out.println("hello"));
Runnable r = new SomeClassThatImplementsRunnable();
doSomething(0, r);