<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>My Blog</title>
	<atom:link href="http://matteomoci.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://matteomoci.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Mon, 09 Nov 2009 00:07:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='matteomoci.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/a482edd071a1e2a3c32842148a3a496b?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>My Blog</title>
		<link>http://matteomoci.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://matteomoci.wordpress.com/osd.xml" title="My Blog" />
	<atom:link rel='hub' href='http://matteomoci.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Inversion of Control</title>
		<link>http://matteomoci.wordpress.com/2009/11/08/inversion-of-control/</link>
		<comments>http://matteomoci.wordpress.com/2009/11/08/inversion-of-control/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 18:04:22 +0000</pubDate>
		<dc:creator>mox601</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://matteomoci.wordpress.com/?p=28</guid>
		<description><![CDATA[Since probably from Tuesday I will be busy studying Spring and other technologies, I&#8217;d like to dive in a bit and see what are the foundations of this platform. Probably this topic will gain deepness with time, but now let&#8217;s start really simple and watch one feature that makes Spring different from EJB: the Inversion [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=matteomoci.wordpress.com&amp;blog=8181097&amp;post=28&amp;subd=matteomoci&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since probably from Tuesday I will be busy studying Spring and other technologies, I&#8217;d like to dive in a bit and see what are the foundations of this platform. Probably this topic will gain deepness with time, but now let&#8217;s start really simple and watch one feature that makes <em>Spring</em> different from <em>EJB</em>: the <em>Inversion of Control</em> pattern.</p>
<p>Imagine to have two classes, <code>Door</code> and <code>Handle</code>. The Door has a private variable that is its Handle, and when it is instantiated with <code>new Door()</code>, it will build a <code>new Handle()</code> inside its constructor as seen in the following diagram.</p>
<p><a href="http://matteomoci.files.wordpress.com/2009/11/door_handle_no_ioc1.jpg"><img class="alignnone size-full wp-image-32" title="door_handle_no_IOC" src="http://matteomoci.files.wordpress.com/2009/11/door_handle_no_ioc1.jpg?w=230&#038;h=184" alt="door_handle_no_IOC" width="230" height="184" /></a></p>
<p>If we set up things in this way, there is a tight coupling between Door and Handle, and probably every change to the Handle class (for example, adding a parameter to the constructor) will affect even the Door class: to prevent this <em>high coupling</em> (class Door is responsible even for the creation of the Handle), we could have a IoC framework, that will have the responsibility to inject the instance of class Handle to the class Door, confining changes of class Door to the IoC framework. Class Door will only have a private variable Handle and a method setHandle(Handle h) to get the reference from the IoC framework.</p>
<p><a href="http://matteomoci.files.wordpress.com/2009/11/door_handle_with_ioc1.jpg"><img class="alignnone size-full wp-image-33" title="door_handle_with_IOC" src="http://matteomoci.files.wordpress.com/2009/11/door_handle_with_ioc1.jpg?w=337&#038;h=212" alt="door_handle_with_IOC" width="337" height="212" /></a></p>
<p>In this case, class Door and Handle are independent, since the IoC framework uses the public method of class Door to set up the Handle.However, there are three different ways to implement the IoC pattern, and every web framework uses its implementation.</p>
<p><em>Setter-based IoC</em>: like in the example I showed before, we use a setter method to inject the referred object (Handle) in the referring object (Door). In this way, even if Handles change a lot during its lifetime in the development, we prevent Doors from changing: the main drawback is that we expose the internal variables with setter methods, violating the key OO principle of classes, that is encapsulation of data.</p>
<p><em>Constructor-based IoC</em>: we use a constructor to set the reference of the object, so that only the creator knows about the referenced object.</p>
<p><em>Interface-based IoC</em>: objects have to implement a specific interface of the IoC framework, so that the framework will be able to properly inject the objects. In this way there is no need for an external configuration file with the objects&#8217; references. This locks your application to a specific IoC framework.</p>
<p>I am sure this approach has a lot of advantages, most of all when you don&#8217;t know much about the referred class and how it will change in the application lifecycle, but i think that in order to fully understand its potential it should be seen in a well defined context, comparing the same web application written with EJB and with Spring that accompanies the IoC pattern with principles of <em>Aspect Oriented Programming</em>. Hope to learn this in the future.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/matteomoci.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/matteomoci.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/matteomoci.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/matteomoci.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/matteomoci.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/matteomoci.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/matteomoci.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/matteomoci.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/matteomoci.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/matteomoci.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/matteomoci.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/matteomoci.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/matteomoci.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/matteomoci.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=matteomoci.wordpress.com&amp;blog=8181097&amp;post=28&amp;subd=matteomoci&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://matteomoci.wordpress.com/2009/11/08/inversion-of-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe35dbd707f8a3892f0275350b3585cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mox601</media:title>
		</media:content>

		<media:content url="http://matteomoci.files.wordpress.com/2009/11/door_handle_no_ioc1.jpg" medium="image">
			<media:title type="html">door_handle_no_IOC</media:title>
		</media:content>

		<media:content url="http://matteomoci.files.wordpress.com/2009/11/door_handle_with_ioc1.jpg" medium="image">
			<media:title type="html">door_handle_with_IOC</media:title>
		</media:content>
	</item>
		<item>
		<title>By reference or by value?</title>
		<link>http://matteomoci.wordpress.com/2009/11/05/by-reference-or-by-value/</link>
		<comments>http://matteomoci.wordpress.com/2009/11/05/by-reference-or-by-value/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 17:36:27 +0000</pubDate>
		<dc:creator>mox601</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[parameters passing]]></category>
		<category><![CDATA[reference]]></category>

		<guid isPermaLink="false">http://matteomoci.wordpress.com/?p=15</guid>
		<description><![CDATA[Brief code example about Java parameters passing with primitive types and classes. <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=matteomoci.wordpress.com&amp;blog=8181097&amp;post=15&amp;subd=matteomoci&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since this is my first post, i will start really small and build from there in the future.</p>
<p>I&#8217;d like to show some code about how java passes arguments between functions. In the first example we will use primitive types (int, float, etc), while in the second we will use instances of some class.</p>
<p>First example:<br />
<code><br />
public void testModifiedString() {<br />
String originalString = "first string";<br />
int originalNumber = 10;<br />
StringModifier test = new StringModifier(originalString, originalNumber);<br />
String beforeMethodOne = test.string + " " + test.number;<br />
System.out.println(beforeMethodOne);<br />
StringModifier.methodOne(test.string, test.number);<br />
String afterMethodOne = test.string + " " + test.number;<br />
System.out.println(afterMethodOne);<br />
assertEquals(afterMethodOne, beforeMethodOne);<br />
}<br />
</code><br />
Where <code>StringModifier</code> has two public fields, <code>string</code> and <code>number</code>, and <code>methodOne(String s, int number)</code> is:<br />
<code><br />
public static void methodOne(String s, int number) {<br />
s = "string modified by methodOne";<br />
number = 0;<br />
}<br />
</code></p>
<p>Everyone can guess the output of this test, right? It will print out<br />
<code><br />
first string 10<br />
first string 10<br />
</code><br />
suggesting us that, in Java, the primitive types are passed by value: <code>methodOne</code> will work on copies of its parameters, not affecting the original ones.</p>
<p>Let&#8217;s go one step further, and let&#8217;s try this with classes. Let&#8217;s take a look at the following short test case:<br />
<code><br />
public void testMoveCircle() {<br />
int origin = 0;<br />
Circle myCircle = new Circle(origin, origin);<br />
int delta = 10;<br />
myCircle.printCoordinates();<br />
CircleMover circleMover = new CircleMover();<br />
circleMover.moveCircle(myCircle, delta, delta);<br />
myCircle.printCoordinates();<br />
assertEquals(origin + delta, myCircle.getX());<br />
assertEquals(origin + delta, myCircle.getY());<br />
}<br />
</code></p>
<p>We build a new Circle in the origin (0, 0) and print its coordinates. Then, we build an object <code>CircleMover</code> that will try to move <code>myCircle</code> to a new place (10, 10) and reprint the coordinates.<br />
The output will be:<br />
<code><br />
coordinates: x = 0 y = 0<br />
coordinates: x = 10 y = 10<br />
</code><br />
as we could guess from the test. But if we look inside the <code>moveCircle</code>&#8216;s body, we notice something we didn&#8217;t expect:<br />
<code><br />
public void moveCircle(Circle circle, int deltaX, int deltaY) {<br />
circle.setX(circle.getX() + deltaX);<br />
circle.setY(circle.getY() + deltaY);</code></p>
<p>//code that tries to assign a new reference to circle<br />
circle = new Circle(0, 0);<br />
circle = null;<br />
}</p>
<p>This means that the method changes the circle&#8217;s coordinates, but also tries to modify the reference to the object passed, building a new Circle. Seeing that we can&#8217;t overwrite the reference circle, we guess that it is just a pointer passed by value to the function.</p>
<p>I published a <a title="gist" href="http://gist.github.com/226211">gist</a> on github if you want to let you see and download the files and/or add tests. Contact me if you find errors or want to share some ideas.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/matteomoci.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/matteomoci.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/matteomoci.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/matteomoci.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/matteomoci.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/matteomoci.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/matteomoci.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/matteomoci.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/matteomoci.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/matteomoci.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/matteomoci.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/matteomoci.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/matteomoci.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/matteomoci.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=matteomoci.wordpress.com&amp;blog=8181097&amp;post=15&amp;subd=matteomoci&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://matteomoci.wordpress.com/2009/11/05/by-reference-or-by-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe35dbd707f8a3892f0275350b3585cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mox601</media:title>
		</media:content>
	</item>
	</channel>
</rss>
