I'd always assumed DI was some really complex design pattern, yet it appears I've been using DI without even knowing it, unless I'm missing something.
Apologies for the link to a Java article, it just happens to be what I was reading:
http://www.javaranch.com/journal/200709 ... 709.jsp#a4
Is that seriously all there is to it? Injecting objects which implement an interface via a setter method? I could have sworn I'd seen more complicated demonstrations of it than that. Hmm...
Dependency Injection
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Passing in the "dependency" itself in the constructor is only *one* of many ways to Inject dependencies
(eg: If you passed in a ResourceLocator in the constructor you would immediately be able to get all dependencies when you need them...)
And then the fun begins with Spring Injection etc because they provide whole libraries to not only inject dependencies, but also load factories that are able to create the actual objects (on which the consumers depend))...
Anyway, as i already mentionned, an article summing up a couple of solutions to solve the problem: http://martinfowler.com/articles/injection.html
(eg: If you passed in a ResourceLocator in the constructor you would immediately be able to get all dependencies when you need them...)
And then the fun begins with Spring Injection etc because they provide whole libraries to not only inject dependencies, but also load factories that are able to create the actual objects (on which the consumers depend))...
Anyway, as i already mentionned, an article summing up a couple of solutions to solve the problem: http://martinfowler.com/articles/injection.html
Last edited by timvw on Fri Sep 14, 2007 6:07 am, edited 2 times in total.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Hmm... I'll have to look into this again thentimvw wrote:Passing in the "dependency" itself in the constructor is only *one* of many ways to Inject dependencies![]()
(eg: If you passed in a ResourceLocator in the constructor you would immediately be able to get all dependencies when you need them...)
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Ah ha, I got it. I was thinking factory classes would be a good way to implement DI. Looking at a Spring configuration excerpt you see this:
So basically the dependencies are listed in XML and I can jump to a fairly confident conclusion that they have some generic factory which glues everything together. Pretty neat I guess 
Code: Select all
<beans>
<bean id="AirlineAgency" class="com.dnene.ditutorial.common.impl.SimpleAirlineAgency" singleton="true"/>
<bean id="CabAgency" class="com.dnene.ditutorial.common.impl.SetterBasedCabAgency" singleton="true">
<property name="airlineAgency">
<ref bean="AirlineAgency"/>
</property>
</bean>
<bean id="TripPlanner" class="com.dnene.ditutorial.common.impl.SetterBasedTripPlanner" singleton="true">
<property name="airlineAgency">
<ref bean="AirlineAgency"/>
</property>
<property name="cabAgency">
<ref bean="CabAgency"/>
</property>
</bean>
</beans>- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland