<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Injecting Those Dependencies</title>
    <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Injecting Those Dependencies</title>
      <description>&lt;p&gt;Last week I was teaching a class with a good group of C++ developers in Chicago. They were up on C++, current standardization efforts, virtual machines, performance analysis and tuning. At one point I mentioned a metric for virtual method dispatch on the &lt;span class="caps"&gt;JVM&lt;/span&gt; and one of the students used my numbers to work backwards to determine the number of instructions on different processors.&lt;/p&gt;


	&lt;p&gt;I was teaching our Working Effectively with Legacy Code class. Michael Feathers usually teaches this class, but he was busy (probably working with legacy code somewhere, which, ironically, is why he wrote the book &amp;#8211; to solve the problem once and for all and to stop working with legacy code).&lt;/p&gt;


In an early project we have some problem code (the class was in Java, but we discussed C++ quite a bit):
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    public void addEvent(Event event) {
        event.added();
        events.add(event);
        mailService.sendMail(&amp;quot;jacques@spg1.com&amp;quot;, &amp;quot;Event Notification&amp;quot;, event
                .toString());
        display.showEvent(event);
    }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

The problem with this code, however, comes from what happens in the constructor:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    public Scheduler(String owner, SchedulerDisplay display) {
        this.owner = owner;

        mailService = MailService.getInstance();
        this.display = new SchedulerDisplay();
    }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The display object is associated with a real device (OK, not in our simulation, but you get the point). We need to fix that dependency.&lt;/p&gt;


So we traditionally talk about a few ways to address that:
	&lt;ol&gt;
	&lt;li&gt;Create an interface and then have a concrete and test-double implementation&lt;/li&gt;
		&lt;li&gt;Create a test subclass&lt;/li&gt;
	&lt;/ol&gt;


Those are a few typical ways to handle this problem, but given that the students also worked in C++ and were very concerned with performance (and dynamic binding), so we discussed more alternatives:
1. Link seam &amp;#8211; build the system using the original SchedulerDispaly header file but link in a different version of the class. 2. Template parameter (compiler seam) &amp;#8211; introduce a template parameter and compile in the dependency rather than link it in.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;#pragma once

template&amp;lt;class D&amp;gt; class Scheduler
{
public:
  Scheduler(D &amp;amp;display);
  ~Scheduler();
  void handleEvent();

private:
  D &amp;amp;display;
};&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

So here are 4 ways to remove the dependency (working bottom up in a sense):
	&lt;ol&gt;
	&lt;li&gt;Link seam: link in a different version of SchedulerDisplay&lt;/li&gt;
		&lt;li&gt;Compiler seam: use a template parameter&lt;/li&gt;
		&lt;li&gt;Dynamic seam: create a test subclass that removes external dependency&lt;/li&gt;
		&lt;li&gt;Dynamic seam: extract an interface from existing concrete class, and make Scheduler depend on the new interface.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;For C++, the first option might be a good option if it allows a very quick build/test cycle. Imagine using a class that brings in several unwanted dependencies. By making a test version replacement and linking to it, you might be able to get up and running quickly. On the other hand, you have to have a custom build target, which is really outside of the language.&lt;/p&gt;


The second option is good for C++ and maybe C#, but not Java. Java&amp;#8217;s implementation of generics is so bad, that it requires an interface for this particular case, so you end up with the final option anyway. Using templates puts the seam into the language rather than the build system. It will affect clients because either:
	&lt;ol&gt;
	&lt;li&gt;They will have to provide a template parameter&lt;/li&gt;
		&lt;li&gt;Or, by providing a default value for the template parameter, clients will be aware of a class they were not previously aware of. Still, a viable option, but it will increase build time at least a little.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;The third option may not work in any language without changing the original problem code. Imagine a no argument constructor that performs some static initialization. Without changing the constructor, you cannot guarantee that this approach will work. In any case, the problem as presented requires a change since the constructor was doing the initialization of the display; it did not allow for dependency injection.&lt;/p&gt;


	&lt;p&gt;The final option requires a bit more change, but is pretty flexible if you can afford the virtual method overhead. In Java this isn&amp;#8217;t much of an issue (for a number of reasons, mostly related to effective &lt;span class="caps"&gt;JIT&lt;/span&gt; optimization). The cost is a bit more of a problem in C++ because introducing a first virtual method is a big deal. Personally, I don&amp;#8217;t work on embedded systems, so I can generally ignore that problem. But this is not something to simply ignore, context does matter.&lt;/p&gt;


	&lt;p&gt;So which option is the best?&lt;/p&gt;


	&lt;p&gt;None of the above.&lt;/p&gt;


	&lt;p&gt;As a developer, you should probably be aware of all of these options (or maybe not the template option of you&amp;#8217;re strictly a Java developer). Then, when you are faced with a legacy coding problem, you&amp;#8217;ll have more tools from which to choose.&lt;/p&gt;</description>
      <pubDate>Mon, 22 Mar 2010 16:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:079869bc-e7ef-4708-9a69-7b24541bd190</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>welc</category>
      <category>refactoring</category>
      <category>dependency</category>
      <category>injection</category>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Gold Price</title>
      <description>&lt;p&gt;With the help of golden prices we can buy all things which we have desire.
&lt;a href="http://www.goldprice.com/" rel="nofollow"&gt;Gold Price&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 30 Nov 2011 12:49:28 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:d49366b3-d2b0-4734-9d56-593d5dcf6099</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-180112</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Brokerage firms</title>
      <description>&lt;p&gt;I am happy when reading your blog with updated information! thanks alot and hope that you will post more site that are related to this site.&lt;/p&gt;</description>
      <pubDate>Fri, 25 Nov 2011 06:13:56 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:26d9b15a-c80e-4f95-ae60-19b54679b2f9</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-178019</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Rubel</title>
      <description>&lt;p&gt;Reading through this your publish I&#8217;ve known many something totally new relating to this new great version from the &#8221; new world &#8221; that we haven&#8217;t known before.
&lt;a href="http://www.nohypeiphone4sfeatures.com" rel="nofollow"&gt;No Hype iPhone 4S Features&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 17 Nov 2011 10:40:41 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:7be1350f-151f-4a20-806f-df350fe7dd9b</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-174836</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Wood Floor Dallas</title>
      <description>&lt;p&gt;Remodeling floors will go a long way in setting the ambiance of the room. Materials are available in a variety of textures and styles such as hardwood flooring, laminate, and ceramics. Materials must be durable and skid resistant to withstand high traffic while still remaining comfortable underfoot. Choice Flooring and Remodelling Brings you the best services yet pocket friendly! &lt;a href="mailto:info@choiceflooringandremodeling.com" rel="nofollow"&gt;info@choiceflooringandremodeling.com&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 11 Nov 2011 01:43:07 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:3e21a7d1-19cb-463a-a13e-f106ddd7a87e</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-171280</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Nascar Racing</title>
      <description>&lt;p&gt;Please email me with any hints &amp;#38; tips about how you made your website look this good, I would appreciate it.&lt;/p&gt;</description>
      <pubDate>Sun, 06 Nov 2011 23:37:17 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:11a3e1b3-366f-4e26-a634-68c91fc224de</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-169144</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by No Hype iPhone 4S Features</title>
      <description>&lt;p&gt;I think the article very interesting and above all true and applicabile.Il blog is well done and nice, congratulations.
&lt;a href="http://www.nohypeiphone4sfeatures.com" rel="nofollow"&gt;No Hype iPhone 4S Features&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 05 Nov 2011 12:41:13 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3f469397-fdf0-48ac-8df4-5d62e310f0a8</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-168728</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by No Hype iPhone 4S Features</title>
      <description>&lt;p&gt;I&#8217;m happy for I found this blog, explaining everything in detail regarding the topic. We can benefit from these articles and I wish we do not have a language barrier.I Will probably be back to get more.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.nohypeiphone4sfeatures.com" rel="nofollow"&gt;No Hype iPhone 4S Features&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 05 Nov 2011 12:38:09 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4cb1b435-41a2-4e06-b04a-b849d2677a9a</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-168726</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by ysbearing/yisong@1stbearing.com</title>
      <description>&lt;p&gt;Slewing ring is also called slewing bearing, some people called: rotary support, swing support. English Name: slewing bearing or slewing ring bearing or turn table bearing, slewing ring in the real industrial applications is very wide. &lt;a href="http://www.1stbearing.com" rel="nofollow"&gt;http://www.1stbearing.com&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 19 Oct 2011 01:24:39 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1436af9d-6952-48ad-a5de-938306823535</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-159295</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Ashley Bowling</title>
      <description>&lt;p&gt;No matter what the problem is,
it&amp;#8217;s always a people problem.&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;Jerry Weinberg&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Sat, 15 Oct 2011 14:19:50 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:44c973a5-ce6d-4521-8079-68ede26247c0</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-157138</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by bakiretr</title>
      <description>&lt;p&gt;Thank you for taking the time to publish this information very useful!I&#8217;m still waiting for some interesting thoughts from your side in your next post&lt;/p&gt;</description>
      <pubDate>Tue, 11 Oct 2011 09:33:08 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:447401f1-626a-4d16-9b3f-96e0f0308aaf</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-154645</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by sex</title>
      <description>&lt;p&gt;Do all these people just need to vent? Well that&#8217;s my two cents! Have a great day!&lt;/p&gt;</description>
      <pubDate>Tue, 11 Oct 2011 09:31:58 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:14958e87-0f84-4904-87a4-5e8570ba96c2</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-154644</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by ????? ??????</title>
      <description>&lt;p&gt;ely. Parkour was disciplined anterior to Freerunning by David Belle. It consists of hurdles and bounds. The big philosophical system&lt;/p&gt;</description>
      <pubDate>Tue, 11 Oct 2011 06:44:23 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ec1aec40-2466-41f2-bf57-ba15a8930af7</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-154533</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by ????? ??????</title>
      <description>&lt;p&gt;Parkour and Freerunning are different but not entirely. Parkour was disciplined anterior to Freerunning by David Belle. It consists of hurdles and bounds. The big philosophical system&lt;/p&gt;</description>
      <pubDate>Tue, 11 Oct 2011 06:44:07 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4e65a556-f00e-48c7-88ac-69bc53029cd2</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-154532</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Louboutins</title>
      <description>&lt;p&gt;asda s
d60f4as5+d6
sss&lt;/p&gt;</description>
      <pubDate>Mon, 10 Oct 2011 22:45:46 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:85e274cd-bad4-4ff9-81a0-2bf814e5a9bb</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-153903</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by moncler</title>
      <description>&lt;p&gt;asdfa sd0+fa+0s d9sdss&lt;/p&gt;</description>
      <pubDate>Mon, 10 Oct 2011 22:41:50 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:52cf4f2f-3c7c-4a99-9c7a-fc4f4fbb395e</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-153885</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Christian</title>
      <description>&lt;p&gt;as vbcn+0v b+vcb n+v&lt;/p&gt;</description>
      <pubDate>Mon, 10 Oct 2011 22:38:25 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:6a4debb6-7aec-45d7-a638-6999dbc8df91</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-153865</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by UGG Montclair</title>
      <description>&lt;p&gt;The article in &lt;a href="http://www.montclairboots.com/" rel="nofollow"&gt;UGG Montclair Boots Sale&lt;/a&gt;your blog reminds me some old memory .That is good .It gives me happy .I think we will ha&lt;a href="http://www.montclairboots.com/" rel="nofollow"&gt;Montclair UGG Boots Sale&lt;/a&gt;ous talk.Do you agree?&lt;/p&gt;</description>
      <pubDate>Thu, 29 Sep 2011 20:01:26 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:38b5fc8e-cd20-4b1e-9904-de13746e1ea0</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-148095</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by http://www.ghalay.com</title>
      <description>&lt;p&gt;I need to read more on this topic&#8230;I admiring time and effort you put in your blog, because it is obviously one great place where I can find lot of useful info..
&lt;a href="http://www.ghalay.com" rel="nofollow"&gt;http://www.ghalay.com&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 27 Sep 2011 17:10:25 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5bc42a09-9876-4c5b-bdf3-ac8453621d07</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-146237</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Latest Movie Poster</title>
      <description>&lt;p&gt;I really enjoyed reading this post. I congte you for the terrific job that you&amp;#8217;re doing. Great stuff, just simply amazing!&lt;/p&gt;</description>
      <pubDate>Thu, 15 Sep 2011 03:27:49 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c59f1fc9-31d3-4448-96fb-e0d3330f7805</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-140520</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by coupon for baby</title>
      <description>&lt;p&gt;So informative this are provided here,I really happy to read this post,I was just imagine about it and you provided me the correct informg,So thanks for sharing the information.&lt;/p&gt;</description>
      <pubDate>Thu, 15 Sep 2011 03:26:13 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:930b4243-f672-4143-a029-1db58e24c8e4</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-140516</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Omaha Foreclosure Central</title>
      <description>&lt;p&gt;I want to say that this article is awesome, nice written and include almost all vital infos. I would like to see more posts like this .&lt;/p&gt;</description>
      <pubDate>Wed, 07 Sep 2011 14:20:37 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4b0acabd-322c-4cf6-91f0-211d9c152ebb</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-138184</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Rent To Own Omaha </title>
      <description>&lt;p&gt;I just wanted to say hi and tell you how wonderful I think your blog is. I am a latecomer, unfortunately-but I will keep a close eye on it,now that I have found it. Take care!!!&lt;/p&gt;</description>
      <pubDate>Tue, 06 Sep 2011 14:28:51 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ae29b6f1-14c7-4097-841e-648cd2e3a01b</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-137736</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by Nebraska Foreclosures Central</title>
      <description>&lt;p&gt;I&amp;#8217;m happy for I found this blog, explaining everything in detail regarding the topic. We can benefit from these articles and I wish we do not have a language barrier.I Will probably be back to get more.&lt;/p&gt;</description>
      <pubDate>Sun, 04 Sep 2011 11:24:19 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4227687b-1137-4598-9358-892737bb6636</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-136843</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by bagsupplyer</title>
      <description>&lt;p&gt;Thank you for posting. Waiting for updating.
&lt;a href="http://www.bagsupplyer.com/DSQ-n787/" rel="nofollow"&gt;Discount designer men DSQ t shirt from China at on line store&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 03 Sep 2011 04:10:07 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e13694a2-a681-4bbb-8cfc-f70b0cc03ffb</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-136685</link>
    </item>
    <item>
      <title>"Injecting Those Dependencies" by bagsupplyer</title>
      <description>&lt;p&gt;Nice Article.Thank you for sharing.
Waiting for updating.
&lt;a href="http://www.bagsupplyer.com/Diesel-n786/" rel="nofollow"&gt;Cheap brand men Diesel t shirt from China for wholesale at on line store&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 03 Sep 2011 04:09:14 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:075a6574-186e-4104-85c6-d57bbe31c187</guid>
      <link>http://blog.objectmentor.com/articles/2010/03/22/injecting-those-dependencies#comment-136684</link>
    </item>
  </channel>
</rss>

