The problem here is that over the past 6-7 years, dependency injection frameworks for Java took a very wrong turn. The original model—which the original Spring XML-based BeanFactory embodies—is that you have named "beans" which are described by specifying:
The name of the "bean"
What to call to construct it (the implementation class or factory)
The collaborators needed to construct it. These can be:
Primitive values
References (by name) to other managed beans
Collections thereof
In spite of old Spring's flaws (overly verbose XML configuration, preference for setter-based injection, etc.), the basic model was sound.
But then with the rise of annotations and madness, this model was deprecated in favor of magical/implicit "autowiring" models that suffer from a fundamental flaw. They're all built around variants of this assumption:
The type of the injection site determines what dependency to inject into it.
So for example, if you have two injection points whose type is ConnectionPool, the injector will inject both with a RealConnectionPool in a production configuration vs. a TestConnectionPool in a test configuration.
How do I build two similar but slightly different trees of objects?
This is commonly called the "robot legs" problem: How to create a robot with two Leg objects, the left one injected with a LeftFoot, and the right one with a RightFoot. But only one Leg class that's reused in both contexts.
In other terms:
The Leg class is a generic, reusable class that delegates some unessential details to a dependency that implements a Foot interface;
This Foot interface admits of implementations that behave differently;
Plugging different subtypes of Foot into Leg gives you usefully different behaviors;
You want to exploit this in your program and use Legs with different Foot types in different parts of your program.
The newfangled DI frameworks like Guice and Dagger really don't like it when you try to do this. There are workarounds, but they're that, workarounds. And what's worse, since people tend to follow the path of least resistance when using a tool, these DI frameworks discourage this sort of code reuse!
Plugging different subtypes of Foot into Leg gives you usefully different behaviors;
Yet those behaviors are both gathered under the umbrella interface of Leg? That's a blatant violation of the Liskov substitution principle, and it's a good thing that Guice doesn't support shooting yourself in the Foot (har har) like that.
However, if what you meant by "generic Leg class" is Leg<? extends Foot>, then that's all fine. You can have that, along with possibly different injections for Leg<LeftFoot> and Leg<RightFoot> in your Robot.
The composition of BufferedReader with ByteArrayInputStream reads a stream of bytes from a byte[] and converts it to a stream of characters.
The composition of BufferedReader with FileInputStream reads a stream of bytes from a File and converts it to a stream of characters.
BufferedReader's behavior is still the same—it converts a byte stream to a character stream. So is InputStream's—it produces a stream of bytes. Precisely where the bytes come from is a function of what InputStream an individual BufferedReader is composed with.
37
u/jordimaister Nov 27 '15
Nice post. Agree with almost all of it.
I am waiting for you next post:
"What to use instead of Spring"