Now, see, this is what balks me. All this fuss just for what?
This seems to just completely erase the meaning of interfaces. Interfaces are there to be used by multiple classes - if you can only offer a single implementation, then why use an interface at all?
This seems to just completely erase the meaning of interfaces.
Yeah, you're right, and the "solution" of @Qualifier annotations sucks a little, because in Java type annotations aren't really part of the type, but it still seems better than XML.
If this situation creeps up a lot in your project, you probably don't want to be using a DI framework for those parts.
I think a lot of times people try to use frameworks for everything, even when they handle the situation poorly. It's perfectly okay to just use it where it's helpful and then take another approach somewhere else (e.g., it's not illegal to just write a Servlet people). I can see how this line of thinking leads people to the conclusion that they should never use frameworks too. I've drawn that conclusion several times myself, but I think it's wrong.
This seems to just completely erase the meaning of interfaces. Interfaces are there to be used by multiple classes - if you can only offer a single implementation, then why use an interface at all?
There's a nuance here; a lot of the examples that are used to justify these new-style DI frameworks assume that you will have multiple implementations of the same interface, but not within the same dependency injection context. A typical example is injecting dummy or mock implementations of your interfaces when unit testing.
This is why I bring up the clown robots example—this is an example where you want to do both of these things:
Instantiate the Leg interface differently in different injection points;
Instantiate more than one Robot with independent choice of leg implementation for each of their legs.
The good old named bean reference graph description file paradigm (which the Spring XML is an implementation of, albeit a far from ideal one) does this trivially. The new ones actively discourage this—which gets in the way of writing or using parametrizable, reusable components.
PS Interfaces are very often a good tool even when you only have one implementation of them. One reason is that they promote separate compilation and decoupling. If you're writing a subsystem for an application, you should strongly consider starting by writing a set of interfaces that its consumers will use to interact with it, and commenting them with your swag of what the contracts of the methods are. Then you go and write the implementation, with a view that you can throw it away and replace it later thanks to the work you put into specifying the interfaces.
8
u/[deleted] Apr 23 '14
Now, see, this is what balks me. All this fuss just for what?
This seems to just completely erase the meaning of interfaces. Interfaces are there to be used by multiple classes - if you can only offer a single implementation, then why use an interface at all?