<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C.M. Jackson .Net &#187; patterns</title>
	<atom:link href="http://www.cmjackson.net/tag/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cmjackson.net</link>
	<description>Web Design, Programming, Tutorials</description>
	<lastBuildDate>Tue, 18 May 2010 22:43:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Dependency Injection</title>
		<link>http://www.cmjackson.net/2010/01/27/dependency-injection/</link>
		<comments>http://www.cmjackson.net/2010/01/27/dependency-injection/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 14:33:10 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Object Oriented Programming]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=358</guid>
		<description><![CDATA[Dependency Injection is a design pattern that is used in Object Oriented Programming to create a relationship between two objects without object A depending on object B. Instead, through the use of an Interface, a place-holder for object B can be referenced in object A. Then, object B can be passed into object A where it is [...]]]></description>
			<content:encoded><![CDATA[<p>Dependency Injection is a design pattern that is used in Object Oriented Programming to create a relationship between two objects without object A depending on object B. Instead, through the use of an Interface, a place-holder for object B can be referenced in object A. Then, object B can be passed into object A where it is utilized. If there is an object C that uses the same interface as object B, it may also be passed into object A.</p>
<p><strong>A Tightly Coupled Relationship</strong><br />
Below is an example of two classes that are tightly coupled, meaning that if we want to replace object B with another class, we must change the code of object A. Object A is dependent on object B to execute.</p>
<pre class="brush: vb;">
Public Class ObjectA

  Private _obj As ObjectB

  Public Sub New(ByVal obj As ObjectB)

    _obj = obj

  End Sub

  Public Sub RunB()

    _obj.Run()

  End Sub

End Class
</pre>
<p> </p>
<pre class="brush: vb;">
Public Class ObjectB

  Public Sub Run()

    Console.WriteLine(&quot;Run from objectB&quot;)

  End Sub

End Class
</pre>
<p><strong>A Loosely Coupled Relationship</strong><br />
Now, let&#8217;s look at a better approach that will allow us to replace ObjectB with another class using the same interface or replace it with a child class of ObjectB.</p>
<p>Below, the ObjectB class now implements the IObjectB interface and the references to ObjectB in ObjectA have been replaced with a reference to the interface IObjectB instead.</p>
<pre class="brush: vb;">
Public Interface IObjectB

  Sub Run()

End Interface
</pre>
<pre class="brush: vb;">
Public Class ObjectA

  Private _obj As IObjectB

  Public Sub New(ByVal obj As IObjectB)

    _obj = obj

  End Sub

  Public Sub RunB()

    _obj.Run()

  End Sub

End Class
</pre>
<pre class="brush: vb;">
Public Class ObjectB
  Implements IObjectB

  Public Sub Run() Implements IObjectB.Run

    Console.WriteLine(&quot;Run from objectB&quot;)

  End Sub

End Class
</pre>
<p>Now, ObjectA can execute no matter if we pass into it a copy of ObjectB or another class that implements the IObjectB interface. The Interface ensures that all objects that implement it will have a subroutine called Run. That is all that ObjectA needs to know. This breaks ObjectA&#8217;s dependency on ObjectB.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2010/01/27/dependency-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
