<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1509083967755245144</id><updated>2012-02-07T20:03:33.549+01:00</updated><category term='hibernate'/><category term='Builder'/><category term='dependency injection'/><category term='dry'/><category term='annotation'/><category term='ant'/><category term='java'/><category term='groovy'/><category term='swing'/><category term='spring'/><category term='iphone problem'/><category term='macwidgets'/><category term='gwt'/><category term='software design'/><category term='polymorphism'/><category term='stubrn'/><category term='ocp'/><category term='timestamp'/><category term='stubs'/><category term='c++'/><category term='scenegraph'/><category term='duck typing'/><category term='difference'/><category term='visitor'/><title type='text'>42 Incorrect Answers</title><subtitle type='html'>My Impressions and Suggestions about Software Development in the 21th Century. Or something like that.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-5919724590886598649</id><published>2010-01-25T12:48:00.002+01:00</published><updated>2012-02-07T12:23:43.286+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='stubrn'/><category scheme='http://www.blogger.com/atom/ns#' term='stubs'/><category scheme='http://www.blogger.com/atom/ns#' term='groovy'/><title type='text'>Stubrn, Maps and Groovy</title><content type='html'>&lt;p&gt;I&amp;#39;ve added a new feature to Stubrn: Instead of implementing an anonymous class
with methods or fields as shown
&lt;a href="/2010/01/stubrn-some-ideas-about-concise-stubs.html"&gt;here&lt;/a&gt;, you can now pass
a &lt;code&gt;Map&amp;lt;String,Object&amp;gt;&lt;/code&gt; as a holder to &lt;code&gt;stubFor()&lt;/code&gt;.&lt;/p&gt;

&lt;a name='more'&gt;&lt;/a&gt;

&lt;p&gt;It will take the key as the method name and the value as the return value of
stubbed method. This will look like: &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;Stubbery&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Stubbery&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;findByName&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;Lists&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newArrayList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;

&lt;span class="n"&gt;EntityDAO&lt;/span&gt; &lt;span class="n"&gt;stub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stubFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EntityDAO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Controller&lt;/span&gt; &lt;span class="n"&gt;sud&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setEntityDAO&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;listEntities&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Yes, i agree, on the first look, this does not appear very concise, but if we
assume now, that we have a Groovy Test, then we get something like&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// this is Groovy&lt;/span&gt;
&lt;span class="n"&gt;Stubbery&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Stubbery&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;EntityDAO&lt;/span&gt; &lt;span class="n"&gt;stub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stubFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EntityDAO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nl"&gt;findByName:&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)]]);&lt;/span&gt;

&lt;span class="n"&gt;Controller&lt;/span&gt; &lt;span class="n"&gt;sud&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setEntityDAO&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;listEntities&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This looks better. And the important fact here is: You can test Java classes
with this approach, hence it might be quite handy. And finally we added
another convenience method to the Stubbery: 
&lt;code&gt;stubFor(Class forclass,String methodName,Object returnValue)&lt;/code&gt; 
which leads us to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;EntityDAO&lt;/span&gt; &lt;span class="n"&gt;stub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stubFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;EntityDAO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;findByName&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;Lists&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newArrayList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;

&lt;span class="n"&gt;Controller&lt;/span&gt; &lt;span class="n"&gt;sud&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setEntityDAO&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;listEntities&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Stubrn lives on &lt;a href="http://github.com/lgirndt/Stubrn"&gt;Github&lt;/a&gt;. You might also
like to &lt;a href="/search/label/stubrn"&gt;browse all articles on Stubrn&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-5919724590886598649?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/5919724590886598649/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=5919724590886598649' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/5919724590886598649'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/5919724590886598649'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2010/01/stubrn-maps-and-groovy.html' title='Stubrn, Maps and Groovy'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-459963372188462565</id><published>2010-01-23T19:30:00.010+01:00</published><updated>2012-02-07T12:54:32.813+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='stubrn'/><category scheme='http://www.blogger.com/atom/ns#' term='stubs'/><title type='text'>Stubrn - Some Ideas about Concise Stubs in Java</title><content type='html'>&lt;h2&gt;Motivation &lt;/h2&gt;

&lt;p&gt;There are a lot of cool Java Mock frameworks out there in the wild. I am a
great fan of JMock myself, but nevertheless, they are Mock frameworks. As a
matter of fact, sometimes i just want to use a Stub and not a Mock. That said,
and not finding a proper solution to fit my needs, i started considering my
demands. If you are as eager as me to see the results of this quest, feel free
to read on!&lt;/p&gt;

&lt;a name='more'&gt;&lt;/a&gt;

&lt;p&gt;Of course, creating a Stub should be straight forward.
Not much boilerplate, not much code duplication. Usually they should simply
let the stubbed class return something that is appropriate in the current
test. How do we do stubbing currently? If we assume we talk about stubbing
interfaces, a quite painless approach would be implementing an anonymous class
of our Interface-to-be-stubbed directly in the test. Hm, this is good, as long
as the interface is quite thin. If we have one which consists of several
methods, and our System Under Test just makes use of a fraction of these
methods, it is more than cumbersome to implement all of them. Hence, a Stub
framework should enable us to just provide what&amp;#39;s required. &lt;/p&gt;

&lt;h2&gt;How to Stub&lt;/h2&gt;

&lt;p&gt;Well, things are explained best by showing examples. So here comes ours: Let&amp;#39;s
assume we have some ... er... Entity :) &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Entity&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Of course a DAO to persist the Entity: &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;EntityDAO&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;findByName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Entity&lt;/span&gt; &lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;store&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;findByAge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;findByAge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;and some client of the DAO, our controller:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;EntityDAO&lt;/span&gt; &lt;span class="n"&gt;entityDAO&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setEntityDAO&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EntityDAO&lt;/span&gt; &lt;span class="n"&gt;entityDAO&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;entityDAO&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;entityDAO&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;listEntities&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;entityDAO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findByName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Yeah, you already know where this leads to, but let me put it explicitly.
Assume the SUD is the controller. Hence when we write a unit test for the
controller, we want to get rid of the whole persistence layer. We don&amp;#39;t want
to use our real EntityDAOImpl, which most likely uses JDBC or some ORM to
access a real database. No sir. We just want to test
&lt;code&gt;listEntities(),&lt;/code&gt; and it is enough for us, that the DAO simply
returns some entities here. It is irrelevant, where they originate from. If we
would stub the interface with an anonymous class, we would have to implement
all the methods. How annoying is that? We know, the controller uses just that
single one. Being a lazy person, i also just want to implement that one! So
what do you think about this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;Stubbery&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Stubbery&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;EntityDAO&lt;/span&gt; &lt;span class="n"&gt;stub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stubFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EntityDAO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;findByName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Lists&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newArrayList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;


&lt;span class="n"&gt;Controller&lt;/span&gt; &lt;span class="n"&gt;sud&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setEntityDAO&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;listEntities&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In the spirit of JMock&amp;#39;s Mockery we have our Stubbery. It is a factory to
create Stubs of the given interface as done by &lt;code&gt;stubFor&lt;/code&gt;. You tell
the Stubbery which interface should be stubbed and provide a &lt;em&gt;holder&lt;/em&gt;,
an object which provides a method with the same signature you want to
implement on the interface. The Stubbery takes care, that there is something
which looks like your interface, and which calls the respective method on the
holder. And now we need to make some steps back. This is surely not the way
polymorphism is done in Java. I am well aware of this, and of course we will
discuss the disadvantages later. But on the other hand, we must be fair. We
already know there are &lt;a
href="http://www.blogger.com/2009/08/different-faces-of-polymorphism.html"&gt;different
forms of polymorphism&lt;/a&gt; and only some of them require explicitly typed
interfaces. So after all, we sacrifice static compiler checks for code
conciseness and need to judge, if this is a fair trade. &lt;br /&gt; If we have
accepted this argumentation, we could go a step further. We want to be even
more concise, hence let us do something like: &lt;br /&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;Stubbery&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Stubbery&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;EntityDAO&lt;/span&gt; &lt;span class="n"&gt;stub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stubbery&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stubFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EntityDAO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="nd"&gt;@ByName&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;findByName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Lists&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newArrayList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;

&lt;span class="n"&gt;Controller&lt;/span&gt; &lt;span class="n"&gt;sud&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setEntityDAO&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sud&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;listEntities&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So what do we do here? Again, the Stubbery creates a Stub by using a holder.
But this time, the holder has no respective method, but only a field, matching
the return type and the name of our desired method. Yes, this is coming close
to dark magic. Hence we&amp;#39;ve added the Annotation, which marks the field
explicitly to be used as the result of method-to-be-stubbed.&lt;/p&gt;

&lt;h2&gt;Limitations and Disadvantages&lt;/h2&gt;

&lt;p&gt;After explaining the core idea, we need to come to the dark side of this nice
concept. Under the hood we use a &lt;code&gt;java.lang.reflect.Proxy&lt;/code&gt;. And the
drawback here is, we can just create Proxies on Interfaces. This means, we can
just stub Interfaces. But again, this is the same limitation Mock frameworks
have to obey to, so we have to live with it. My main concern has already been
mentioned. The way the holder is associated with stubbed interface is not
related at all to any way Java allows polymorphism, and the most sad
consequence of this fact is, that it will not be affected by refactoring. If
you change the signature of the interface, nobody knows that your test
contains some holder object, which makes strong assumptions about the details
of that signature. Once changed, it does not match anymore, hence your stub
will not work. But there&amp;#39;s always a But. Our But says: It&amp;#39;s the same for
dynamically typed languages. Those fail not at compile time, but only at test
time. And since we talk about Stubs, which live in test world, it might be
acceptable to fail just during test time! &lt;br /&gt; &lt;/p&gt;

&lt;h2&gt;Final Words&lt;/h2&gt;

&lt;p&gt;So much of the basic idea. I&amp;#39;ve implemented a prototype and called it
&lt;b&gt;Stubrn&lt;/b&gt;. It could be found at &lt;a
href="http://github.com/lgirndt/Stubrn"&gt;Github&lt;/a&gt; and is currently in an
early version, but those concepts introduced here work basically. So if you
have any further concerns or improvements, don&amp;#39;t hesitate and let me know. You
might also like to &lt;a href="http://www.blogger.com/search/label/stubrn"&gt;browse all articles on
Stubrn&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-459963372188462565?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/459963372188462565/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=459963372188462565' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/459963372188462565'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/459963372188462565'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2010/01/stubrn-some-ideas-about-concise-stubs.html' title='Stubrn - Some Ideas about Concise Stubs in Java'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-9002673684708693822</id><published>2009-08-14T10:47:00.002+02:00</published><updated>2012-02-07T11:04:06.117+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='gwt'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='annotation'/><title type='text'>Marrying GWT with Spring the Generic Way</title><content type='html'>During the last days I've played around with GWT and Spring. Although there are already plenty of blog posts describing how to tunnel GWT Services through a Spring DispatcherServlet, they have all one disadvantage in common: They require to do a lot of explicit Spring Bean Definitions. In this post I describe a nice mechanism to automatically create those Bean Definitions, freeing the Bean Config of redundant, boilerplate code. &lt;br /&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span class="fullpost"&gt;Putting Spring into a GWT application is a good idea! Besides several other aspects which make dependency injection a &lt;em&gt;Great Thing&lt;/em&gt;, it also simplifies the handling of Service Implementations on the server side. In a vanilla GWT application it is assumed, that each implementation inherits &lt;code&gt;com.google.gwt.user.server.rpc.RemoteServiceServlet&lt;/code&gt;, which results in a lot of configuration in your &lt;code&gt;web.xml&lt;/code&gt;.  &lt;/span&gt;&lt;br /&gt;
&lt;span class="fullpost"&gt;  This problem has already been identified other people, for instance &lt;a href="http://docs.google.com/Doc?docid=dw2zgx2_25492p5qxfq&amp;amp;hl=en"&gt;Richard Bondi describes&lt;/a&gt; how to introduce a &lt;code&gt;org.springframework.web.servlet.DispatcherServlet&lt;/code&gt; to solve this discrepancy. This is a good first step, because it allows us to implement our Service Implementations independent of RemoteServiceServlet (Loosely Coupling is always a good idea, esp. with respect to testability), but it just shifts redundant configuration from the former &lt;code&gt;web.xml&lt;/code&gt; to our Spring Bean Configuration. &lt;br /&gt;
If i may quote the above cited article, we end up with: &lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;pre class="xml" name="code"&gt;&lt;span class="fullpost"&gt;&amp;lt;bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&amp;gt;
       &amp;lt;property name="mappings"&amp;gt;
         &amp;lt;value&amp;gt;
         /**/login.whatever=loginController
         /**/userProfile.whatever=userProfileController
         &amp;lt;/value&amp;gt;
       &amp;lt;/property&amp;gt;
     &amp;lt;/bean&amp;gt;

     &amp;lt;bean name="loginController" class="com.aspentech.imos.servlet.GWTController"&amp;gt;
       &amp;lt;property name="remoteService"&amp;gt;
         &amp;lt;bean class="com.foo.gwt.login.server.LoginServiceImpl" /&amp;gt;
       &amp;lt;/property&amp;gt;
     &amp;lt;/bean&amp;gt;

     &amp;lt;bean name="userProfileController" class="com.aspentech.imos.servlet.GWTController"&amp;gt;
       &amp;lt;property name="remoteService"&amp;gt;
         &amp;lt;bean class="com.foo.gwt.login.server.UserProfileService" /&amp;gt;
       &amp;lt;/property&amp;gt;
     &amp;lt;/bean&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;span class="fullpost"&gt;Do you see what i am talking about? Defining the controller beans is just boilerplate, defining the mapping is redundant. This might be anything, but it is surely not DRY. Could we improve it? I think yes, let me show you how.    &lt;/span&gt;&lt;br /&gt;
&lt;h2&gt;
&lt;span class="fullpost"&gt;Prerequisites&lt;/span&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="fullpost"&gt;There is a GWT application. &lt;/span&gt;&lt;/li&gt;
&lt;span class="fullpost"&gt;
&lt;li&gt;We have a &lt;code&gt;GwtController&lt;/code&gt; as described by Richard Bondi.&lt;/li&gt;
&lt;/span&gt;&lt;/ul&gt;
&lt;h2&gt;
&lt;span class="fullpost"&gt;Annotating the Service Implementation&lt;/span&gt;&lt;/h2&gt;
&lt;span class="fullpost"&gt;My idea is to  &lt;ol&gt;
&lt;li&gt;Automatically find those classes which are supposed to be service implementations. &lt;/li&gt;
&lt;li&gt;Automatically create the entries of the &lt;code&gt;SimpleUrlHandlerMapping&lt;/code&gt;.  &lt;/li&gt;
&lt;/ol&gt;
Finding some classes at runtime is a proper use case for Annotations. We mark those classes with a distinct Annotation, and search them on the classpath at runtime. If we add some configuration values, like service endpoint name, we could also create the url handler mapping. Let's see what GWT already gives us: &lt;pre class="java" name="code"&gt;@RemoteServiceRelativePath("greeting.service")
    public interface GreetingService extends RemoteService {
     String greetServer(String name); 
    }  
&lt;/pre&gt;
Ok, we have an Annotation and yes, oha, it defines (a part of) the Service Endpoint. No, it is not our implementation, but only the pure service interface. Maybe we could use this, then search for classes implementing this interface ... arg, hang on ..., if we look closer, we see that &lt;code&gt;@RemoteServiceRelativePath&lt;/code&gt; is actually of &lt;code&gt;RetentionPolicy.CLASS&lt;/code&gt;, which in turn means it is not available in the VM at runtime. Damn, as long as &lt;a href="http://code.google.com/p/google-web-toolkit/issues/detail?id=3803&amp;amp;sort=-id&amp;amp;start=100"&gt;this is not changed&lt;/a&gt;, we cannot use the Annotation. &lt;br /&gt;
Nonetheless, doing the job with Annotations is still a good idea. Hence the second best solution is writing our own: &lt;br /&gt;
&lt;pre class="java" name="code"&gt;@Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface RemoteServiceImplementation {
     String value();
    }
&lt;/pre&gt;
Which is the applied to our Service Implementation &lt;pre class="java" name="code"&gt;@RemoteServiceImplementation("/**/greeting.service")
    public class GreetingServiceImpl implements
      GreetingService {

     public String greetServer(String input) {
      return "Hello, " + input + ". How are you?";
     }
    }
&lt;/pre&gt;
As you see, the Annotation value is our endpoint mapping entry. Yes, this violates DRY, but maybe we have to accept it for now.   &lt;h2&gt;
Searching for Annotations&lt;/h2&gt;
&lt;a href="http://bill.burkecentral.com/2008/01/14/scanning-java-annotations-at-runtime/"&gt;Bill Burke describes very detailed&lt;/a&gt; the problems with respect to scanning for Annotations on the classpath. His consequence is &lt;a href="http://scannotation.sourceforge.net/"&gt;Scannotation&lt;/a&gt;, a small framework for looking up annotated classes without bloating the VM's Permanent Generation. He forgets to mention that Scannotation depends on &lt;code&gt;javassist&lt;/code&gt;, but now u are warned. &lt;br /&gt;
As a matter of fact I've encapsulated this scanning behind a strategy to enable better testing, it finally looks like &lt;pre class="java" name="code"&gt;import java.io.IOException;
    import java.util.Collection;
    
    public interface AnnotationScanner {

     Collection&amp;lt; String &amp;gt; scanClassPathForAnnotation(String annotationName)
     throws IOException;
    }
&lt;/pre&gt;
And &lt;pre class="java" name="code"&gt;import java.io.IOException;
    import java.net.URL;
    import java.util.Collection;
    
    import javax.servlet.ServletContext;

    import org.scannotation.WarUrlFinder;
    import org.scannotation.AnnotationDB;
        
    import org.springframework.web.context.ServletContextAware;

    public abstract class WarAnnotationScanner 
    implements AnnotationScanner,ServletContextAware {
        
        private ServletContext servletContext;

     public void setServletContext(ServletContext servletContext) {
      this.servletContext = servletContext;  
     }

     private URL createClasspath() {
      if(servletContext==null) throw new NullPointerException();
      return WarUrlFinder.findWebInfClassesPath(servletContext);
     }

     public Collection&lt;string&gt; scanClassPathForAnnotation(
             String annotationName) throws IOException {

      URL url = createClasspath();
      AnnotationDB annotationDB = new AnnotationDB();

      annotationDB.setScanClassAnnotations(true);
      annotationDB.setScanFieldAnnotations(false);
      annotationDB.setScanMethodAnnotations(false);
      annotationDB.setScanParameterAnnotations(false);

      annotationDB.scanArchives(url); 

      return annotationDB.getAnnotationIndex().get(annotationName); 
     }
    }
&lt;/string&gt;&lt;/pre&gt;
&lt;h2&gt;
Automatically Creating Bean Definitions&lt;/h2&gt;
How do we do this? Again, there is &lt;a href="http://www.carlobonamico.com/blog/2008/01/22/how-to-dynamicallyprogrammatically-define-spring-beans/"&gt;a good article by Carlo Bonamico&lt;/a&gt; about this topic. Similar to him, we will also implement &lt;code&gt;org.springframework.beans.factory.config.BeanFactoryPostProcessor&lt;/code&gt;, since we need to get some other classes inject. The drawback of this approach is, that we have to make a downcast of &lt;code&gt;ConfigurableListableBeanFactory&lt;/code&gt; to &lt;code&gt;BeanDefinitionRegistry&lt;/code&gt;. This is a violation of OCP, but again we don't get around it. The main idea of the &lt;code&gt;ServiceUrlMappingRegistrationBean&lt;/code&gt; is to generate all those Spring Beans we mentioned above programmatically. &lt;pre class="java" name="code"&gt;import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;

import com.google.common.collect.Maps;

public class ServiceUrlMappingRegistrationBean 
implements BeanFactoryPostProcessor  {
 
 private static final Logger log = Logger.getLogger(ServiceUrlMappingRegistrationBean.class);
 
 private Class controllerClass;

 private AnnotationScanner annotationScanner;
 
 public void setAnnotationScanner(AnnotationScanner annotationScanner) {
  this.annotationScanner = annotationScanner;
 }
 
 public void setControllerClass(String controllerClassName) 
 throws ClassNotFoundException {
  this.controllerClass = 
   getClass().getClassLoader().loadClass(controllerClassName);
 }

 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
   throws BeansException {
  try {
   
   final BeanDefinitionRegistry registry = 
    (BeanDefinitionRegistry) beanFactory;   
   final Map&lt;string, string=""&gt; mapping = Maps.newHashMap();
      
   for(String className : scanClassPathForRemoteServices()) {
    
    final String beanName = toBeanName(className);
    final Class remoteServiceclass = 
     getClass().getClassLoader().loadClass(className);    
    registry.registerBeanDefinition(
      beanName,createServiceBeanDefinition(remoteServiceclass));
    
    final String servicePath = extractServicePath(remoteServiceclass);
    mapping.put(servicePath,beanName);
    
    log.debug("Register " + className + " with " 
      + servicePath + "=" + beanName);
   }
   registry.registerBeanDefinition(
     "urlMapping", createUrlMappingBeanDefinition(mapping));
   
  }catch(IOException e){
   log.error("Could not scan annotated classes.",e);
  }catch(ClassNotFoundException e){
   log.error("Class not found.",e);
  }
 }
 
 private String extractServicePath(Class clazz){
  RemoteServiceImplementation annot = 
   clazz.getAnnotation(RemoteServiceImplementation.class);
  return annot.value();
 }
 
 private String toBeanName(String className){
  return className;
 }
 
 private BeanDefinition createServiceBeanDefinition(
   Class remoteServiceclass) throws ClassNotFoundException{
  
  RootBeanDefinition beanDef = new RootBeanDefinition(this.controllerClass);  
  RootBeanDefinition remoteServiceBean =
   new RootBeanDefinition(remoteServiceclass);
  
  BeanDefinitionHolder holder =
   new BeanDefinitionHolder(remoteServiceBean, "");
  
  MutablePropertyValues values = new MutablePropertyValues();
  values.addPropertyValue("remoteService", holder);
  beanDef.setPropertyValues(values);
  
  return beanDef;
 }
 
 private BeanDefinition createUrlMappingBeanDefinition(
   Map&lt;string, string=""&gt; mapping){
  RootBeanDefinition beanDef = 
   new RootBeanDefinition(SimpleUrlHandlerMapping.class);
 
  MutablePropertyValues values = new MutablePropertyValues();
  values.addPropertyValue("mappings",mapping);  
  beanDef.setPropertyValues(values);
  return beanDef;
 }
 
 private Collection&amp;lt; String &amp;gt; scanClassPathForRemoteServices() 
 throws IOException{
  if(annotationScanner == null) 
   throw new NullPointerException("AnnotationScanner");
  
  String annotName = RemoteServiceImplementation.class.getName();    
  return annotationScanner.scanClassPathForAnnotation(annotName);  
 }

}
&lt;/string,&gt;&lt;/string,&gt;&lt;/pre&gt;
Which finally ends up in the following Bean Definition &lt;pre class="xml" name="code"&gt;&amp;lt;bean class="raidr.server.spring.ServiceUrlMappingRegistrationBean"&amp;gt;
  &amp;lt;property name="controllerClass"
            value="raidr.server.servlet.GWTController" /&amp;gt;
  &amp;lt;property name="annotationScanner"&amp;gt;
            &amp;lt;bean class="raidr.server.spring.WarAnnotationScanner"/&amp;gt;
        &amp;lt;/property&amp;gt; 
 &amp;lt;/bean&amp;gt;
&lt;/pre&gt;
Isn't it beautiful?  &lt;h2&gt;
Finally&lt;/h2&gt;
In this article we described a mechanism to automatically create bean definitions for GWT Services dispatched by the DispatcherServlet by adding some homegrown Annotations. There are two known limitations: We duplicate Service End Point Description in the Annotations and we violate OCP by making a downcast to the BeanDefinitionRegistry.  &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-9002673684708693822?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/9002673684708693822/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=9002673684708693822' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/9002673684708693822'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/9002673684708693822'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/08/marrying-gwt-with-spring-generic-way.html' title='Marrying GWT with Spring the Generic Way'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-2205717261380631326</id><published>2009-08-07T00:11:00.008+02:00</published><updated>2010-02-21T10:31:45.745+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='timestamp'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='difference'/><title type='text'>Transparent Handling of Timestamp Differences with Hibernate</title><content type='html'>&lt;p&gt;
SQL does not define unified methods to calculate differences between timestamp values. Many database vendors provide somehow their own solution for this problem, but when it comes to generalizing the persistence layer with Hibernate, we are faced again with the missing transparent approach. This article shows a concise solution proven to work transparently on Oracle 10g and HSQL. 
&lt;/p&gt;
&lt;span class="fullpost"&gt;
&lt;p&gt;
In our case, we had
&lt;ol&gt;
    &lt;li&gt;A model class, let's call it Task. Task has two fields startTime &amp; endTime, both of type java.sql.Timestamp. And several other fields of different primitive types.
    &lt;li&gt;Task was subject to a Grails application (Although the persistence layer was not GORM, but plain Hibernate with Spring due to several other reasons).
    &lt;li&gt;We had to display lists of tasks, paginated and orderable by either all fields or &lt;em&gt;the duration&lt;/em&gt;, which is actually the difference between endTime and startTime.
    &lt;li&gt;In production mode the data is stored in an Oracle 10g Database
    &lt;li&gt;Unit &amp; Integration Tests were done with an In Memory HSQL Database.
&lt;/ol&gt;
These requirements implied the following constraints:
&lt;ul&gt;
    &lt;li&gt;We have to avoid redundancy. Storing the duration also as a field of Task is not an option. If it could be calculated, then it has to be calculated.
    &lt;li&gt;We want to handle pagination and ordering of the fields and duration similar. We could have introduced some dispatching logic, like if duration is not envolved we use query A otherwise we use query B. But that's not a proper solution and most likely not very well maintainable. We just need a single query which can cope with both cases. Ultimately this means, we have a property name and tell Hibernate to order the result according to this property.
    &lt;li&gt;We need mechanism to cope with Oracle &amp; HSQL.
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;
The solution we found consists of several aspects. Let's go through them top down:
&lt;/p&gt;

&lt;h2&gt;Adding a property formula to the Hibernate Mapping&lt;/h2&gt;
Since we have to handle Hibernate properties and the calculated value in the same way, we need to add duration as an "artificial property" to the Hibernate Mapping of Task. Luckily Hibernate provides a mechanism for this use case: Properties could consist of a &lt;code&gt;formula&lt;/code&gt;, which is basically the call to a valid HQL function. We simply need to define
&lt;pre name="code" class="xml"&gt;
&amp;lt;hibernate-mapping&amp;gt;
    &amp;lt;class name="foo.Task" table="TASKS"&amp;gt;
        ...
        &amp;lt;property 
            name="duration" 
            formula="MY_DIFF_TIME_SECONDS(START_DATE, END_DATE)" 
        /&amp;gt;
        ...
    &amp;lt;/class&amp;gt;
&amp;lt;/hibernate-mapping&amp;gt;
&lt;/pre&gt; 

What do we do here? We assume there is a registered function called &lt;code&gt;MY_DIFF_TIME_SECONDS(x,y)&lt;/code&gt;, which calculates the difference between two timestamps in seconds. This method is called with Task's properties &lt;code&gt;START_DATE&lt;/code&gt; and &lt;code&gt;END_DATE&lt;/code&gt;.

&lt;h2&gt;Adding custom functions to the Hibernate Dialects&lt;/h2&gt;
&lt;p&gt;
We have expected that &lt;code&gt;MY_DIFF_TIME_SECONDS(x,y)&lt;/code&gt; exists. Since it is not given by an Intelligent Creator for free, we need to take care that such a function will be created. Hibernate itself does actually the same thing. There are different Hibernate Dialects for different database vendors. Those dialects register several types and function which have individual names in the database with a unified name for Hibernate. We just have to apply this to our function as well. 
&lt;/p&gt;
&lt;pre name="code" class="java"&gt;
    public class MyOracle10gDialect extends Oracle10gDialect {

     public MyOracle10gDialect()
     {
      super();
      registerFunction(
          "MY_DIFF_TIME_SECONDS", 
       new SQLFunctionTemplate(
           Hibernate.INTEGER, 
           "MY_DIFF_TIME_SECONDS(?1,?2)"));
     }
    }
&lt;/pre&gt;
This is the example of our custom tailored Oracle Dialect. We extend Hibernate's Dialect, make sure that super() is called and just add our new function. We do the same with &lt;code&gt;MyHsqlDialect&lt;/code&gt; by extending &lt;code&gt;HSQLDialect&lt;/code&gt; but just don't show it here.

&lt;h2&gt;Implementing the functions in the Database&lt;/h2&gt;
&lt;p&gt;
Up to now we've created the unified facade for the Java world using Hibernate, now we need to look at the other side: Calculating timestamp differences in both Databases. This is, as outlined above, the actually problem we are facing. It is possible to do that in Oracle and HSQL, but with two completely different approaches. But since we have unified everything from Hibernate's point of view, we simply have to take care that the databases get a function &lt;code&gt;MY_DIFF_TIME_SECONDS(x,y)&lt;/code&gt; to calculate our timestamp differences.
&lt;/p&gt;
&lt;p&gt;
Let's do it for Oracle first:
&lt;pre name="code"&gt;
    CREATE OR REPLACE FUNCTION MY_DIFF_TIME_SECONDS(
        start_date IN DATE,end_date in DATE)
    return NUMBER
    IS
    BEGIN
      if start_date is null 
      then 
        return null;
      end if;
      if end_date is null 
      then 
        return null;
      end if;
      return (end_date - start_date)*24*60*60;
    END;
    /
&lt;/pre&gt;
This is PL/SQL and it does what we need. Besides some defensive null checks we simply utlize the Minus operater, which is also defined on Oracle's date types. Its result is a fraction of a day, hence we need to shift the result to become our difference in seconds.
&lt;/p&gt;
&lt;p&gt;
HSQL does not know PL/SQL (lucky dude!). But it enables us to add arbitrary Java functions which could be called from SQL. Fine thing:

&lt;pre name="code" class="java"&gt;        
    import java.sql.Timestamp;
    import org.joda.time.Period;
    
    public class HsqlStoredFunctions {

     /**
      * Returns the difference between two timestamps in seconds.
      * 
      * @param start the early time
      * @param end the later time
      * @return if start and end != null then the difference is returned,
      *         otherwise null is returned
      * 
      */
     public static Integer diffTimeInSeconds(Timestamp start,Timestamp end){
      if(start == null){
       return null;
      }  
      if(end == null){
       return null;
      }

      Period period = new Period(start.getTime(),end.getTime());

      return period.toStandardSeconds().getSeconds();
     }
    }
&lt;/pre&gt;
This is the function. It has to be static, but that's actually the only requirement. We make it aware to HSQL by:

&lt;pre name="code" class="xml"&gt;
    public class HsqlDataSourceInitializer implements InitializingBean{

     private JdbcTemplate jdbcTemplate;
     public void setDataSource(DataSource dataSource){
      this.jdbcTemplate = new JdbcTemplate(dataSource);
     }

     @Override
     public void afterPropertiesSet() throws Exception {

      String aliasDiffTime = "CREATE ALIAS MY_DIFF_TIME_SECONDS FOR " +
        " \"mypackage.testing.HsqlStoredFunctions.diffTimeInSeconds\"";
      jdbcTemplate.execute(aliasDiffTime);
     }
    }
&lt;/pre&gt;

Here we see actually two quite interesting features. First the function is registered with the shown SQL statement. Secondly we can also see &lt;em&gt;when&lt;/em&gt; we do this registration. Just reconsider: We use HSQL only in our testing scenario. In our particular case the whole persistence layer (and all other layers of the application as well) is nicely decoupled via Spring. Hence the difference between the production and the test environment is the Spring Bean configuration of the DataSource. But since the whole setup is now silently down by Spring we need a hook to register our function when the In Memory Database is created. Therefore &lt;code&gt;HsqlDataSourceInitializer&lt;/code&gt; is a Spring Bean which implements &lt;code&gt;InitializingBean&lt;/code&gt;. The method &lt;code&gt;afterPropertiesSet()&lt;/code&gt; is called after all Spring Beans have been instantiated. Since the DataSource of HSQL Database is nothing but another Spring Bean, we can safely assume that it is up and running.

&lt;h2&gt;Setting the sort order on Hibernate Criteria Queries&lt;/h2&gt;
Now we have a brief look on how we set the order of Hibernate Criteria Queries to show that we have our required unified solution. We've got this nice helper class:
&lt;pre name="code" class="java"&gt; 
    final class CriteriaUtil {

     private static final String ORDER_ASC = "asc";
     private static final String ORDER_DESC = "desc";

     public final Criteria withOrder(Criteria criteria,String sort,String order){

      if(criteria == null){
       throw new NullPointerException();
      }

      if(order != null){
       if(!(order.equals(ORDER_ASC) || order.equals(ORDER_DESC) )){
        throw new IllegalArgumentException("Unknown order direction: " + order);
       }
      }

      if(sort != null){
       if(order != null &amp;&amp; order.equals(ORDER_DESC)){
        criteria.addOrder(Property.forName(sort).desc());
       }
       else {
        criteria.addOrder(Property.forName(sort).asc());
       }
      }
      return criteria;
     }
    }
&lt;/pre&gt;
And we can use the attributes as provided by Grails' build-in pagination mechanism to extend our Criteria Queries.

&lt;h2&gt;Finally&lt;/h2&gt;
Although we've just shown a solution for Oracle and HSQL, this approach should be applicable to other Databases as well, as long as it is possible to register custom functions and to calculate the timestamp difference in terms of these functions.
&lt;/p&gt;
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-2205717261380631326?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/2205717261380631326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=2205717261380631326' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/2205717261380631326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/2205717261380631326'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/08/transparent-handling-of-timestamp.html' title='Transparent Handling of Timestamp Differences with Hibernate'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-2278086470183309936</id><published>2009-08-04T15:33:00.006+02:00</published><updated>2009-08-08T11:49:19.506+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='polymorphism'/><category scheme='http://www.blogger.com/atom/ns#' term='software design'/><category scheme='http://www.blogger.com/atom/ns#' term='groovy'/><category scheme='http://www.blogger.com/atom/ns#' term='duck typing'/><title type='text'>The Different Faces of Polymorphism</title><content type='html'>&lt;p&gt;
Others might think polymorphism is the skill used by Odo from Deep Space Nine when he shifts his form. But we computer geeks now it better. Polymorphism is the one stop show that makes writing software so much fun. Or to put in another way, it provides us with the power of generalization, loosely coupling and ultimately maintainability. But polymorphism does not just come in the shape of the too well known interfaces with its precious virtual methods. Different paradigms provide us with different mechanisms to wield the Sword of the Many Forms. In this article we will give an introduction to several of these incarnations. Namely C++'s compile time polymorphism and that what they call Duck Typing in Dynamic Typed Languages.
&lt;/p&gt;
&lt;a href="http://www.flickr.com/photos/annabananabobaloo/3137527285/"&gt;&lt;img src="http://farm4.static.flickr.com/3227/3137527285_9053a73a9c_d.jpg"&gt;
&lt;/a&gt;
&lt;span class="fullpost"&gt;
&lt;p&gt;
Let's start with the basic question: What the hell is polymorphism? It is the ability to specify behavioral contracts on the one hand and to fulfill them on the other. You can say what you expect from someone by defining an contract. He in turn could realize your contract however he wants, as long as he sticks to your defined expectations. With this simple mechanism we basically created the founding of what makes software development possible in the large scope.
&lt;/p&gt;
Let's start simple.
&lt;p&gt;
&lt;h2&gt;Runtime Polymorphism&lt;/h2&gt;
Yeah, we are familiar with this one. It's our daily work. Let us just exercise through it. Here is our contract: 
&lt;pre name="code" class="java"&gt;
    public interface Phone {
        void call(String phoneNumber);
    }
&lt;/pre&gt;
We simply expect a Phone. If someone provides us something fulfilling the Phone's demands, we can make a call:
&lt;pre name="code" class="java"&gt;
    public final class Client {
        void makePhoneCall(Phone phone){
            phone.call("555-6832");
        }
    }
&lt;/pre&gt;
Then there is a very simple Phone implementation to complete the example
&lt;pre name="code" class="java"&gt;
    public final class ToyPhone implements Phone {
        public void call(String phoneNumber){
            System.out.println("Ring Ring");
        }
    }
&lt;/pre&gt;
And finally the full scenario breaks down to:
&lt;pre name="code" class="java"&gt;
    Phone phone = new ToyPhone();
    Client client = new Client();
    client.makePhoneCall(phone);
&lt;/pre&gt;

All in all not sophisticated, but just all we need to explain the general idea. You see: With the interface we define our contract. Our Client relies on that we give him something like a Phone, then he will just use it. How does it work? This is due to the way Java implements public methods. Actually they are virtual methods, which means, somewhere under the hood there is a vtable, which maps a class' method to a memory address where the implementation of that method could be found. This is all static runtime information. From an execution flow's point of view calling a method means getting the right address from the vtable and then jump there and continue (if we leave out compiler optimization...). Static means, that the compiler already knows where to look into the vtable, but it will contain the proper value only at runtime, of course. This is no magic at all...
&lt;/p&gt;

&lt;h2&gt;Compile Time Polymorphism&lt;/h2&gt;

Ah ha. Now it is getting more exciting. Let's play the same game, just with C++ meta programming:
&lt;pre name="code" class="cpp"&gt;
    template&amp;lt;typename PHONE&amp;gt; class Client {
    public: 
     void makePhoneCall(PHONE&amp; phone){
      phone.call("555-3223");
     }
    };

    class MobilePhone {
    public: 
     void call(const std::string&amp; phoneNumber);
    };

    void MobilePhone::call(const std::string&amp; phoneNumber){
     std::cout &amp;lt;&amp;lt; "Hello?" &amp;lt;&amp;lt; std::endl;
    }

    int main(){
     Client&amp;lt;MobilePhone&amp;gt; client;
     MobilePhone phone;
     client.makePhoneCall(phone);
    }
&lt;/pre&gt;

&lt;p&gt;
Let's see what we've got here. We see the client, yes that's fine. Ok, this is a templatized class, maybe we would not have expected it, but we just accept it for now. What else? Yes, the implementation of our Phone. This is a C++ class. But, wait: &lt;code&gt;call()&lt;/code&gt; is NOT virtual, what the heck... hm let's wait a further second and finish the example. Ok, &lt;code&gt;main()&lt;/code&gt; will run our example. And it does what we expect: Create an instance of the client, create an instance of the phone and then let the client do his call. But something is still odd... Yes! Where is the so called contract? It's not there!? And this compiles? Obviously yes... Hm... Now we owe some explanations! 
&lt;/p&gt;
&lt;p&gt;
What we see here is called meta programming used by the modern C++ guys and has been first described by Andrei Alexandrescu in  &lt;a href="http://books.google.com/books?id=aJ1av7UFBPwC&amp;lpg=PP1&amp;dq=modern%20c%2B%2B%20design&amp;pg=PP1#v=onepage&amp;q=&amp;f=false"&gt;Modern C++ Design&lt;/a&gt;. He explains how to utilize C++ template compiler for writing programs rather than the actual C++ compiler. Of course this blog post cannot satisfy all which has to be said about meta programming, but just so much: In our particular case we just imply an interface. You and I know, there is nothing which defines our contract explicitly. Psst! But the template compiler does not know that. It just knows that it creates a template instance for the type MobilePhone, and that PHONE and therefore MobilePhone is called on the method &lt;code&gt;call()&lt;/code&gt;. While compiling the instantiated code the C++ compiler in turn does its usual job. MobilePhone has such method, hence everything works out fine. 
&lt;/p&gt;
&lt;p&gt;
I've heard people argue "What's that nonsense! Templates are for generalizing on types! Like the STL does it!" But think about it: Yes templates are used for generalization. For sure. But in this case we don't generalize on types, we generalize on &lt;em&gt;behaviour&lt;/em&gt;. And it turns out more than fine. Are there any benefits? Yes! We've already mentioned the most crucial one: There are no vcalls involved. No vcalls means, the C++ compiler could apply plenty of its optimization strategies, which results in aweful cool runtime behaviour. Eager to see more? &lt;a href="http://www.boost.org/"&gt;Boost&lt;/a&gt; and &lt;a href="http://www.cgal.org/"&gt;CGAL&lt;/a&gt; are awesome examples of modern C++ design.
&lt;/p&gt;
&lt;p&gt;
Actually the C++ guys find this concept of compile time polymorphism that attractive, that they try to incorporate it into a future C++ standard under the term &lt;a href="http://en.wikipedia.org/wiki/Concepts_%28C%2B%2B0x%29"&gt;Concepts&lt;/a&gt;. Although they've currently postponed it from C++0x.
&lt;/p&gt;
&lt;h2&gt;Duck Typing&lt;/h2&gt;
Finally we come to our Dynamic Typed Languages. Here they say, if it talks like a duck and walks like a duck then it is a duck. What does this mean? Have a look.
&lt;p&gt;
The important aspect here is, besides others, that an instance is not checked for its type during compile time as it is done with Java or C++ for example, but only on runtime, just at the moment a method is called on that instance. For me as a Java Enthusiast it's obvious to give an example in Groovy:
&lt;/p&gt;
&lt;pre  name="code" class="java"&gt;
    class Client {
        def makePhoneCall(def phone){
            phone.call('555-3126')
        }
    }
    
    class PhoneBooth {
        def call(def phoneNumber){
            println('Beep...')
        }
    }
    
    static def main(){
        def client = new Client()
        def phone = new PhoneBooth()
        client.makePhoneCall(phone)
    }
&lt;/pre&gt;
&lt;p&gt;
It's nearly the same game as with C++ meta programming. We have no explicit definition of our contract. This is just stated implicitly. But since method dispatching is done just when we execute &lt;code&gt;call()&lt;/code&gt; on &lt;code&gt;phone&lt;/code&gt;, and it's instance PhoneBooth owns such a method, everything works out fine again. But why is it helpful? Because it yields efficient code. Of course, yes, there are a lot of reasons why it is helpful to have a proper static type system. I will not deny it. I also favor it of course. But there are a certain amount of valid use cases, which have other desires, like implementation efficiency. Consider a unit test for example. Maybe you have decoupled everything loosely enough. Your class under test just knows other interfaces. But you still have to create stubs or mocks for those. This is acceptable of course (at least better than to avoid it), but even then it is boiler plate. It does not support the readability of your test. On the other hand, you can easily craft a stub for your interface out of maps and closures. Very dense code, very readable.
&lt;/p&gt;
&lt;h2&gt;Finally&lt;/h2&gt;
What have we learned today? Polymorphism is cool. And besides this there are different paradigms having different styles of polymorphism. Some have explicitly defined contracts, others don't. But they all obey to the same principle. A contract defines expectations. Implementations fulfill this contract.
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-2278086470183309936?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/2278086470183309936/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=2278086470183309936' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/2278086470183309936'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/2278086470183309936'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/08/different-faces-of-polymorphism.html' title='The Different Faces of Polymorphism'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-4869985381865968883</id><published>2009-07-21T20:13:00.013+02:00</published><updated>2012-02-07T13:23:44.127+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Builder'/><category scheme='http://www.blogger.com/atom/ns#' term='software design'/><title type='text'>Builder Pattern - The Swiss Army Knife of Object Creation, Part 2</title><content type='html'>&lt;p&gt;Are you still eager to learn how we use the Builder Pattern in Java to build
hierarchical structures? Like the Groovy People, but without the dynamic
Groovyness? In &lt;a
href="/2009/07/builder-pattern-swiss-army-knife-of.html"&gt;my
last article&lt;/a&gt; I prepared everything we need for today. Hence take your pen
and paper and follow me into the auditorium. The others are already waiting
for us...&lt;/p&gt;

&lt;a name='more'&gt;&lt;/a&gt;

&lt;p&gt;Let&amp;#39;s just sum up, &lt;a
href="/2009/07/builder-pattern-swiss-army-knife-of.html"&gt;what
we&amp;#39;ve learned last time&lt;/a&gt;: Object creation it not always easy. There are
classes in the wild that need some special attention while being plugged
together. When it comes to the extreme this might blur our focus on the code
and the ultimate consequence is: It looses its maintainability. The Builder is
proud to offer his helps to us. By encapsulating the actual construction our
attention is pushed back to &lt;em&gt;what&lt;/em&gt; we create, not &lt;em&gt;how&lt;/em&gt; we
create it.&lt;/p&gt;

&lt;p&gt;But actually this was just some introduction. Our real goal is finding a
declarative way to describe complex structures. Like the Groovy People. But
without the Groovyness, only plain old static Java.&lt;/p&gt;

&lt;p&gt;Our job for today is declaratively constructing some complex structure. What
is a complex structure? I think something hierarchical should be sufficiently
complex for our explanations. And the mother of all hierarchical structures is
the Document Object Model, of course. Or for the slow people: &amp;quot;We are doing
XML, kids!&amp;quot;.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s create this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;head/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
       &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;42 Incorrect Answers&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
       Just click this 
       &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http://42-incorrect-answers.blogspot.com/&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;link&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;For the sake of education we are doing it the hard way and construct it with
&lt;code&gt;org.w3c.dom&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;html&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;head&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;body&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="n"&gt;h1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;h1&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createTextNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;42 Incorrect Answers&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createTextNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Just click this &amp;quot;&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

&lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAttribute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;href&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http://42-incorrect-answers.blogspot.com/&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createTextNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;link&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Yes, it is not the end of the world. But it is a very good example what we&amp;#39;ve
defined above. We surely learn &lt;em&gt;how&lt;/em&gt; we create XML documents, but have
you seen what we create? Although I have written it, I just cannot remember
anymore.&lt;/p&gt;

&lt;p&gt;On the other hand, what about this?&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;XmlDocumentBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;XmlDocumentBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;beginElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;html&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;beginElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;head&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endElement&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;beginElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;body&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;beginElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;h1&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;42 Incorrect Answers&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endElement&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Just click this &amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;beginElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;attr&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;href&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http://42-incorrect-answers.blogspot.com/&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;link&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endElement&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endElement&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endElement&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Up to now we don&amp;#39;t know anything about the XmlDocumentBuilder, but its like a
punch in your face: You see exactly what we are doing here! There&amp;#39;s one thing
I have to admit: We &amp;quot;misused&amp;quot; indention. But that&amp;#39;s the secret of declaration:
If we declare hierarchical structures, we have to write hierarchical code!
Meanwhile you must be quite eager to see how we do this super cool declaration
magic. Best would be, we sneak through it, step by step:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;XmlDocumentBuilder&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Element&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;XmlDocumentBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DocumentBuilderFactory&lt;/span&gt; &lt;span class="n"&gt;docBuilderFactory&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;ParserConfigurationException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;document&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;docBuilderFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newDocumentBuilder&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;newDocument&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; 
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Element&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;XmlDocumentBuilder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;ParserConfigurationException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DocumentBuilderFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newInstance&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So far there&amp;#39;s nothing magical. We build a Document, therefore we need to
remember it. And, as my late grand father used to say: If there is hierarchy,
then there&amp;#39;s a stack.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;XmlDocumentBuilder&lt;/span&gt; &lt;span class="nf"&gt;beginElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

    &lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// if stack empty, then we have the root element&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
       &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;elem&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;peek&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;elem&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;elem&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;XmlDocumentBuilder&lt;/span&gt; &lt;span class="nf"&gt;endElement&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;No matching beginElement()&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Yes, this is our working horse: On Groovy they do hierarchy with closures. Since there are none so far in Java, and maybe also not quite soon, there&amp;#39;s no way, but we have to find our own method to say something has started and something has ended. We do it by saying something has started and something has ended. (That&amp;#39;s easy, does it always work like this?) Starting means: We need a new Element and add it to its parent. Either the current head of the stack, or in the initial case, the document itself. Then we push the Element on the stack and wait for further things to happen. Once everything is done in between, &lt;code&gt;endElement()&lt;/code&gt; just needs to pop our current element from the stack. &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;XmlDocumentBuilder&lt;/span&gt; &lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Empty stack&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;peek&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;setAttribute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;XmlDocumentBuilder&lt;/span&gt; &lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;XmlDocumentBuilder&lt;/span&gt; &lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Empty Stack&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;peek&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createTextNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And these are those nodes, which operate on the current stack&amp;#39;s head. Either
we add attributes or some text nodes. Quite simple.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;XmlDocumentBuilder&lt;/span&gt; &lt;span class="nf"&gt;element&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;beginElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;endElement&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And finally an example of some convience method: If there&amp;#39;s just an element,
we don&amp;#39;t need this begin/end nonesense, or at the least the Client does not.
All the Client wants is just getting his job done. And we help him with it.
Sometimes we are so kind...&lt;/p&gt;

&lt;p&gt;Basically that&amp;#39;s all. We can do Declaration in Java, if we exploit the
hierarchy by explicitly demarcating the start and the end of structures. And
this is only the Children&amp;#39;s Version of it. Consider Immutable Structures: You
just have one chance to set everything. And that&amp;#39;s in the constructor. Can we
do this with a builder as well? Yes we can. We just need a somehow more
sophisticate Stack. In this case the Stack&amp;#39;s Element type must be Wrapper
structure, which could temporarily hold all the values we finally want to put
into the Immutable Object. Only in the respective end method, we pop this
intermediate Wrapper from the stack, get its content, push that into
constructor of our just right now created, immutable object and finally put it
where it belongs.&lt;/p&gt;

&lt;p&gt;One question still remains: Where could we use it? As a matter of fact we&amp;#39;ve
been silent about a very important aspect so far: When the Groovy People talk
about DSLs (they do this, when they talk about their Builder), they really
talk about configuration. Using DSLs is often used in the context of
configuring something. If this is a post compile time configuration - if it is
supposed to be done by the user and not when the binaries are built - then our
whole fancy Builder is nearly worth nothing, since we have to compile it. But
on the other hand, if we consider something like the SwingBuilder, where we
create static structures, than it&amp;#39;s our game.&lt;/p&gt;

&lt;p&gt;Furthermore I&amp;#39;ve already encountered other use cases. For instance consider
Unit Tests. Let&amp;#39;s take the XmlDocumentBuilder. Just think of some class which
does XML binding. It gets an XML document and spills out a model class. Its
Unit Test will need XML Documents at some point in time. You could put a real
XML file as an artifact into the classpath. But that&amp;#39;s just not cool. Because
the Unit Test, and the tested data are in two different places (at least 2
different files). Somewhen in future this will deviate. The closer things are
together, the less the risk that errors sneak in. Hence if we could easily
write the XML Document in code, then we are on the winner side.&lt;/p&gt;

&lt;p&gt;Here you can find the sources of the &lt;a
href="http://code.google.com/p/fourty-two-incorrect-answers/source/browse/trunk/code/src/fourty2ia/builder/XmlDocumentBuilder.java"&gt;XmlDocumentBuilder&lt;/a&gt;
and his companion, the &lt;a
href="http://code.google.com/p/fourty-two-incorrect-answers/source/browse/trunk/code/test-src/fourty2ia/builder/XmlDocumentBuilderTest.java"&gt;XmlDocumentBuilderTest&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This should be enough for now. Feel free to leave your comment and come back
in your spare free time. There&amp;#39;s always free beer around, and sometimes even
some good ideas.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-4869985381865968883?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/4869985381865968883/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=4869985381865968883' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/4869985381865968883'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/4869985381865968883'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/07/builder-pattern-swiss-army-knife-of_21.html' title='Builder Pattern - The Swiss Army Knife of Object Creation, Part 2'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-9154162457676446087</id><published>2009-07-16T18:46:00.021+02:00</published><updated>2009-08-08T11:50:34.754+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Builder'/><category scheme='http://www.blogger.com/atom/ns#' term='software design'/><title type='text'>Builder Pattern - The Swiss Army Knife of Object Creation</title><content type='html'>It comes out that one of my new hobbys is feeding and taking care of unpopular design patterns. Up to just recently the Builder Pattern was one of this unloved creatures, but the enormous success of Groovy's awesome transformation of a single Design Pattern to a complete new world of DSLs converted the Builder to everybody's new Darling. This is the first article in a series of some, which discusses the Builder Pattern in static typed Languages like Java. This article gives a brief introduction to the Builder Pattern in general.

&lt;a href="http://www.flickr.com/photos/sharynmorrow/47086098/"&gt;&lt;img src="http://farm1.static.flickr.com/32/47086098_fc9d562355.jpg" width="500" height="375"/&gt;&lt;/a&gt;

&lt;span class="fullpost"&gt;
With Groovy's dynamic typed nature, we are able to declaratively construct structures &lt;a href="http://groovy.codehaus.org/Swing+Builder"&gt;like this&lt;/a&gt;

&lt;pre  name="code" class="java"&gt;
def swing = new SwingBuilder()

def sharedPanel = {
     swing.panel() {
        label("Shared Panel")
    }
}

count = 0
def textlabel
def frame = swing.frame(
    title:'Frame', 
    defaultCloseOperation:JFrame.EXIT_ON_CLOSE, 
    pack:true, show:true) {
    vbox {
        textlabel = label("Click the button!")
        button(
            text:'Click Me',
            actionPerformed: {
                count++
                textlabel.text = "Clicked ${count} time(s)."
                println "Clicked!"
            }
        )
        widget(sharedPanel())
        widget(sharedPanel())
    }
}
&lt;/pre&gt;

In Groovy's ecosystem this concept nearly became a new religion. Domain models are declared, rather than manually plugged together.  &lt;a href="http://www.grails.org/"&gt;Grails&lt;/a&gt; and &lt;a href="http://groovy.codehaus.org/Griffon"&gt;Griffon&lt;/a&gt; showcase a mutltiverse of applications. Beginning from configuring Database connections, Spring Beans even good ol' Log4j up to whole GUIs via SwingBuilder in Griffon. Today we declare software. And that's ok. Because it eases reading, writing and understanding.

&lt;p&gt;

The point is, is this only possible, if you utilize those new, fancy dynamic typed languages? Couldn't we build structures the declarative way even in our old, poor static languages?  It would be sad, if the answer is no. The world is full of legacy application, Joe the &lt;del&gt;Plumber&lt;/del&gt; Coder has to work with. Even poor Joe should have the oppertunity to write cool code. Hence gladly the answer is yes. But guys, to be honest, it is not that straight 'Just implement BuilderSupport' road. The path I show you gets dirty, filled with stacks and temporary structures, but is definitely  worth the effort. (And since I'm always honest to you: We don't even get there today, but just the next time...)

&lt;p&gt;

But first things first: Let's talk about the basics of the Builder Pattern. We should have the same understanding, at least. The Builder Pattern is one of the so called Creational Patterns. It has one single purpose, and that's creating structures. But it does it with some super awesome fancyness.  To see how it does it, we need to make one step back and have to reconsider why we actually need help in constructing objects. Surely, instantiating a plain &lt;code&gt;new foo.Bar()&lt;/code&gt; is for beginners. But we are talking here about complex structures. Let's make up some &lt;i&gt;real&lt;/i&gt; example. Have you ever handcrafted &lt;a href="http://java.sun.com/javase/6/docs/api/java/awt/GridBagLayout.html"&gt;java.awt.GridBagLayout&lt;/a&gt;? The last time I did, I was pretty annoyed about building the GridBagConstraints:
&lt;pre  name="code" class="java"&gt;
JComponent content = getContent();
GridBagConstraints c11 = new GridBagConstraints();
c11.gridx = 1;
c11.gridy = 1;
c11.weighty = 1.0;
c11.weightx = 1.0;

c11.fill = GridBagConstraints.HORIZONTAL;
c11.anchor = GridBagConstraints.FIRST_LINE_START;

backgroundPanel.add(content,c11);
&lt;/pre&gt;
Do you see what happens? Maybe not on the first look (yes, you do, you are a pro, but please do so as if you didn't), because it is very noisy. What we actually wanted to do is adding our content component to the backgroundPanel. Therefore we have to have to create the GridBagConstraints instance, and assign those constraints we want to apply to our content component. But since GridBagConstraints is a dull Pojo, we need to set each value by hand. This means, for each constraint we want to set, we need to write &lt;code&gt; c11.someConstraint = someContraintValue &lt;/code&gt;. Yes of course, it could be worse, like we could have to implement it with punch cards, but that's what I just called noisy. When I want to construct something, I want to have it so obvious and redundant free as possible. Constantly repeating &lt;code&gt;c11. = &lt;/code&gt; does not kill anyone, but it's also not the best idea we can come up with. 
&lt;p&gt;
Ok, this is just one of the problems we have here. The next one jumps in our face where we set &lt;code&gt;fill&lt;/code&gt; and &lt;code&gt;anchor&lt;/code&gt;. and here the annoyance factor is already huger. GridBagLayout is as the rest of Swing from Java's Pre Enum Era (As a matter of fact even before Josh Bloch discovered the &lt;a href="http://java.sun.com/developer/Books/effectivejava/Chapter5.pdf"&gt;Type Safe Enum Pattern&lt;/a&gt;): Enumeration Values are encoded as Int Constants. This is bad but it gets worth: GridBagConstraints hosts several different Enumerations in the same scope. On one hand there are those values related to fill, like &lt;code&gt;HORIZONTAL&lt;/code&gt; and there are enum values considering anchor like &lt;code&gt;FIRST_LINE_START&lt;/code&gt;. Since they are intermixed in the same Naming scope, namely GridBagConstraints, auto complete is no help at all, when it comes to guessing what to assign to &lt;code&gt;fill&lt;/code&gt;. You have to look up in the API Docs to see your options. Nothing flaws coding more then breaking your flow of coding if you really have to step into Documentation. And finally you can argue: What's that nonsense with setters? That's why POJOs have Constructors, just use them, and your whole argumenation vanishes. Maybe, but actually i don't like these types of Constructors 

&lt;pre name="code" class="java"&gt;
GridBagConstraints(
int gridx, int gridy, int gridwidth, int gridheight, 
double weightx, double weighty, int anchor, int fill, 
Insets insets, int ipadx, int ipady) 
&lt;/pre&gt;

Just imagine an instantiation and then your desire to maintain it. Which one was gridwidth? The first or the second? Mh, let's look into Javadocs, and then count the parameters... No, sorry Sir. I waste my time differently.

But up to now I've promised much, and proofed nothing. So be so kind and have a look:

&lt;pre name="code" class="java"&gt;
backgroundPanel.add(
    getContent(),
    new GbcBuilder()
        .grid(1,1)
        .weight(1.0,1.0)
        .fillHorizontal()
        .anchorLastLineEnd()
        .build() );
&lt;/pre&gt;

Do you see now what I meant with noise? Nothing of it left. When we get to building the constraints, GbcBuilder comes up to our expectations. We even did some helpful things here: Grid values are only assigned in pairs, why bother with two lines if we could have one? Do you see any arbitrary Int Constants anywhere around? No? Auto complete just offers us &lt;code&gt;fillHorizontal()&lt;/code&gt;. 'Huh, fill &amp; horizontal, i guess that's what i need, let's take it.'

And how did we do this it? Let me show you:

&lt;pre name="code" class="java"&gt;
public class GbcBuilder {

    private GridBagConstraints constraints;

    public GbcBuilder(){
        constraints = new GridBagConstraints();
    }

    public GridBagConstraints build(){
        return constraints;
    }

    public GbcBuilder grid(int gridX,int gridY){
        constraints.gridx = gridX;
        constraints.gridy = gridY;
        return this;
    }

    public GbcBuilder weight(double x, double y){
        constraints.weightx = x;
        constraints.weighty = y;
        return this;
    }

    public GbcBuilder fillHorizontal(){
        constraints.fill = GridBagConstraints.HORIZONTAL;
        return this;
    }

    public GbcBuilder anchorLastLineEnd(){
        constraints.anchor = GridBagConstraints.LAST_LINE_END;
        return this;
    }
}
&lt;/pre&gt;

This is actually a super short version of a GridBagConstraintsBuilder. A proper solution would be a little bit huger. Nevertheless, you already see the important aspects. The builder does what he promises: He builds what he should, holds the instance internally and finally spills it out. By constantly returning itself, the Builder gives us this fancy mechanism to repetitively call him. Quite useful feature. 

But I've already mentioned the drawback. Providing a builder is expensive in terms of implementation time. Is does not pay out if we talk about very specialized structures, which might be initialized only once in your code. But for your working horse model classes, which are used everywhere around the corner, maybe this is quite helpful.

This should be enough for now. Today we've learned some interesting things, namely the Groovy People love the Builder, they have invented whole Domain Specific Languages with it. Furthermore the Builder helps us with the proper construction of those structures, which are complex to fill by encapsulating the knowledge how to fill them.

Although I've promised, I have not explained how to build complex hierarchies of objects, like we've seen in Groovy and there are even some noteworthy words about Builders and Immutability to say. But we will keep this for the next time. I hope you come back.

&lt;p&gt;

Here you can find &lt;a href="http://42-incorrect-answers.blogspot.com/2009/07/builder-pattern-swiss-army-knife-of_21.html"&gt;Part 2&lt;/a&gt;.
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-9154162457676446087?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/9154162457676446087/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=9154162457676446087' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/9154162457676446087'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/9154162457676446087'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/07/builder-pattern-swiss-army-knife-of.html' title='Builder Pattern - The Swiss Army Knife of Object Creation'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://farm1.static.flickr.com/32/47086098_fc9d562355_t.jpg' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-8327631757699950899</id><published>2009-06-23T23:01:00.003+02:00</published><updated>2009-06-25T22:23:02.102+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='iphone problem'/><title type='text'>Problems starting apps on the iPhone 3GS</title><content type='html'>I had suddenly a very strange problem. While being in the video app, the phone suddenly hung up, the boot (?) apple logo appeared, nothing happened, a few seconds later i was in the unlock screen.

From that point on I could not start any downloaded apps anymore (evernote, tap tap revenge, twitterific and so forth). I've resetted the settings to default, this haddened helped. Then I uninstalled one of those apps, downloaded it again from the app store and now everything runs smooth again.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-8327631757699950899?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/8327631757699950899/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=8327631757699950899' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/8327631757699950899'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/8327631757699950899'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/06/problems-starting-ipone-3gs-apps.html' title='Problems starting apps on the iPhone 3GS'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-1871760554040095489</id><published>2009-06-21T22:06:00.005+02:00</published><updated>2009-07-15T21:49:39.920+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='swing'/><category scheme='http://www.blogger.com/atom/ns#' term='macwidgets'/><category scheme='http://www.blogger.com/atom/ns#' term='scenegraph'/><title type='text'>Playing around with Java Scenegraph and Swing</title><content type='html'>I've just invested *some* hours to play around with

&lt;ul&gt;
&lt;li&gt;&lt;a href=""&gt;scenegraph&lt;/a&gt;
&lt;li&gt;&lt;a href="http://code.google.com/p/macwidgets/"&gt;macwidgets&lt;/a&gt;
&lt;/ul&gt; 

The results are pretty amazing, in my honest opinion. Basically I've got a SGPanel with an underlying SGNode tree. Most of the nodes are FXComponents and therefore contain swing components again. I did a quick screencast to demo the implemented effects. Currently I can slide pages to any direction in an iphone like style and overlay other components, in this case a semi transparent dialog, which has some fancy fade out effects. All in all quite cool for good ol' swing.

&lt;p&gt;

&lt;object width="500" height="315"&gt;&lt;param name="movie" value="http://www.youtube.com/v/y2LBiAtcxhA&amp;hl=de&amp;fs=1&amp;rel=0&amp;border=1"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/y2LBiAtcxhA&amp;hl=de&amp;fs=1&amp;rel=0&amp;border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="315"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-1871760554040095489?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/1871760554040095489/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=1871760554040095489' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/1871760554040095489'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/1871760554040095489'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/06/playing-around-with-java-scenegraph-and.html' title='Playing around with Java Scenegraph and Swing'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-5960508195791317155</id><published>2009-06-18T18:26:00.004+02:00</published><updated>2009-08-08T11:51:19.793+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='software design'/><category scheme='http://www.blogger.com/atom/ns#' term='visitor'/><title type='text'>Visiting the Family - An Advanced Application of the Visitor Pattern</title><content type='html'>If we want to apply the Visitor Pattern in a complex class hierarchy, the plain Visitor allows us only to visit those classes, which are leafs in the inheritance tree. In this article I'll show a fancy mechanism to overcome this limitation. Not yet bored? Then dive into my explanations.

&lt;span class="fullpost"&gt;
&lt;p&gt;
    
This is the second post in a loosely scheduled series of articles about the Visitor Pattern. Please find the &lt;a href='/2009/06/hail-to-visitor-pattern.html'&gt;first one here&lt;/a&gt;.

&lt;p&gt; 

One of those things we've learned &lt;a href='/2009/06/hail-to-visitor-pattern.html'&gt;last time&lt;/a&gt; is the simple fact, that the Visitor will only visit "leafs" in a complex class hierarchy. Otherwise the polymorphic type dispatching will not work. Well, but we are sometimes faced with situations, where this limitation is not acceptable. Let's look at some fancy example, to illustrate my explanations. And since we all spent our two years military service with the imperial guards, here's an obvious taxonomy of different star wars vehicle types.

&lt;a href="http://www.flickr.com/photos/39558693@N03/3635824455/" title="visitor_hierarchy"&gt;&lt;img src="http://farm4.static.flickr.com/3611/3635824455_49620f2a7f_o.jpg" width="500" height="432" alt="visitor_hierarchy" /&gt;&lt;/a&gt;

Usually complex type systems consist of several levels of abstraction, like our example. Here we have three: 
&lt;ol&gt;
&lt;li&gt; Either everything is the same, namely a Vehicle
&lt;li&gt; or it is a Vehicle Class like StarShip or Walker, 
&lt;li&gt; or it is a concrete Vehicle like, well ... the BWing, for instance. 
&lt;/ol&gt; 
I think you get the point. With our current knowledge we could only visit concrete vehicles. But what do we do if we are a fleet commander and want to command all of our units to start moving. And moving means StarShips start flying and Walker go walking, have a look:

&lt;pre name="code" class="java"&gt;        
    // StarShips...
    public abstract class StarShip extends Vehicle{

        public void fly(){
            System.out.println("Yeaahhh, I'm flying, Baby.");
        }
    }    
    
    // Walker
    public abstract class Walker extends Vehicle{

        public void walk(){
            System.out.println("I'm going for a walk.");
        }
    }    
&lt;/pre&gt;

As a fleet commander we know for sure that we can utilize the Visitor Pattern to command our
&lt;code&gt;Vehicle [] fleet = { new BWing(), new XWing(), new AtAt() }&lt;/code&gt;. 

&lt;p&gt;

A fleet lieutenant would do it like this:
&lt;pre  name="code" class="java"&gt;
    // clumsy fleet lieutenant's way to command his units
    public class ClumsyVisitor implements Visitor{
        public void visit(BWing bwing) {
            bwing.fly();
        }

        public void visit(XWing xwing) {
            xwing.fly();
        }

        public void visit(AtAt atat) {
            atat.walk();
        }
    }    
&lt;/pre&gt;
But we would not have become commander if we would unnecessarily repeat ourselves. As you've already realized, this is code duplication since we don't operate on the correct level of abstraction. The proper way is calling methods on the Vehicle Classes, not on the concrete Vehicles. But again, how to we do that? Here's the solution:

&lt;pre name="code" class="java"&gt;
    public abstract class AbstractVehicleClassVisitor
            implements Visitor {

        public abstract void visit(Walker walker);
        public abstract void visit(StarShip starShip);

        public void visit(BWing bwing) {
            visit((StarShip) bwing);
        }

        public void visit(XWing xwing) {
            visit((StarShip) xwing);
        }

        public void visit(AtAt atat) {
            visit((Walker) atat);
        }
    }    
&lt;/pre&gt; 

Tata: We've just introduced the proper level of abstraction to our Visitor as well by deriving a new type from Visitor, the AbstractVehicleClassVisitor. Or to be more precise, we defined the missing &lt;code&gt;visit(Walker)&lt;/code&gt; and &lt;code&gt;visit(StarShip)&lt;/code&gt; methods and delegate the 'leaf visits' to these new abstract methods. Visiting Vehicle Classes is now done by extending the AbstractVehicleVisitor. Hence our fleet commander command simply looks like:

&lt;pre name="code" class="java"&gt;
    // that's how we command!
    public class MoveVisitor 
    extends AbstractVehicleClassVisitor{

        public void visit(Walker walker) {
            walker.walk();
        }

        public void visit(StarShip starShip) {
            starShip.fly();
        }
    }  
&lt;/pre&gt;

Calling for the troops breaks down to

&lt;pre name="code" class="java"&gt;
    Vehicle [] fleet = { new BWing(), new XWing(), new AtAt() };
    for(Vehicle unit : fleet) {
        unit.accept(new MoveVisitor());
    }
    
    /* Which would output
    Yeaahhh, I'm flying, Baby.
    Yeaahhh, I'm flying, Baby.
    I'm going for a walk.
    */
&lt;/pre&gt;

Yep, that's all. Easy, isn't?
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-5960508195791317155?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/5960508195791317155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=5960508195791317155' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/5960508195791317155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/5960508195791317155'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/06/visiting-family-advanced-application-of.html' title='Visiting the Family - An Advanced Application of the Visitor Pattern'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-8541548122620997613</id><published>2009-06-16T20:48:00.002+02:00</published><updated>2009-06-16T20:52:34.052+02:00</updated><title type='text'>My Two Cents about Coding in English</title><content type='html'>Jens Schauder &lt;a href='http://blog.schauderhaft.de/2009/06/16/language-matters-a-lot/'&gt;blogged about English as the Lingua Franca&lt;/a&gt; in Software projects. He concludes that non native speakers should force themselves into doing their work in English to compete in a more and more international market.

&lt;p&gt;

After working for three years in an international project I absolutely agree with his opinion. Having no other chance to communicate with my japanese and indian colleagues than in english, the first few weeks were really hard. But I've learned several things: 
&lt;ol&gt;
&lt;li&gt; It does not matter whether you speak 100% grammatically correct. It's more important to talk fluent and confident rather than being sure, that every grammar rule you've learned 10 years ago is correctly applied. My impression is people respect me because of my technical reputation, not because I do speak absolutely error free. (I doubt that I do, this blog gives many proofs)

&lt;li&gt; The more you do things, the better you get. And the more confident you get. As Jens said, it's right, start reading things english, write things in english, and if you have the chance, do speak as much english as you can.
&lt;/ol&gt;

&lt;p&gt;

But meanwhile I've also seen the other side of the medal: If your code is commented in your mother tongue - or even more worse: Implemented in your mother tongue - then you've got a more or less limited market of people how can replace you. If the code is in english you extend the potential candidates to the rest of the industrialized world. And if you now consider that people in east europe, asia and india cost about a fourth of your salary, then you might start worrying. Currently I do not worry about these aspects, but if you were stuck in your job for 20 years, have not learned contemporary technologies then you might not be attractive for a modern, agile market and maybe you have a different opinion about this topic. But on the other hand, if you are one these people, you might not read blogs as well :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-8541548122620997613?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/8541548122620997613/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=8541548122620997613' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/8541548122620997613'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/8541548122620997613'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/06/my-two-cents-about-coding-in-english.html' title='My Two Cents about Coding in English'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-1090126559727808516</id><published>2009-06-11T22:21:00.011+02:00</published><updated>2009-06-11T23:18:41.188+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='software design'/><category scheme='http://www.blogger.com/atom/ns#' term='visitor'/><category scheme='http://www.blogger.com/atom/ns#' term='ocp'/><title type='text'>Hail to the Visitor Pattern</title><content type='html'>In my opinion the Visitor Pattern is one of the often underestimated patterns
from the GOF book. For instance in Head First Design Patterns it just gets it
share in the orphaned further patterns section. But applied correctly, the
Visitor is a powerful tool helping you to dispatch complex type systems
without violating the Open Closed Principle. Interested? Then continue reading!

&lt;p&gt;

Let's fetch up those of you, who are not familiar with the Visitor at all.
This pattern enables injecting methods into types. 'Why the hell, do I want to
do this?' you might ask. Well, consider you have some micky mouse type
hierarchy of geometry related classes:

&lt;p&gt;

&lt;img src='http://yuml.me/diagram/scruffy/class/%5BGeometry%5D%5E%5BLine%5D,%20%5BGeometry%5D%5E%5BCircle%5D,%20%5BGeometry%5D%5E%5BPolygon%5D'&gt;&lt;/img&gt;

&lt;p&gt;

And you want to add functionality to these classes, like drawing. You could
add a purely virtual method &lt;code&gt;draw()&lt;/code&gt; to the super class and provide
an implementation in all of the sub classes, since all of our subclasses are
drawn differently, of course. Yes, indeed you could do this. But what if such
a method would introduce dependencies you damn want to avoid with your plain
classes? To stay in our example, let's assume we want draw our Geometries with
QT. Hence we would implement QT depending code in &lt;code&gt;draw()&lt;/code&gt;. This
would make our classes dependent of QT. But what if we don't want this?
Because we also want to allow drawing it in AWT? what now? Add
&lt;code&gt;drawMeWithAWT()&lt;/code&gt; as well? And then add new dependencies? I don't
know about you, but surely I don't want this! Therefore, what else can we do.

&lt;p&gt;

Ah, I've got an idea. There must have been a reason, why god (or at least
James Gosling) has invented &lt;code&gt;instanceof&lt;/code&gt; in Java:

&lt;pre name="code" class="java"&gt;
public void drawGeometry(Geometry g){
    
    if(g instanceof Polygon){
        drawPolygon((Polygon) g);
    }else if(g instanceof Circle){
        drawCircle((Circle) g);
    }else if(g instanceof Line){
        drawLine((Line) g);
    }else {
        throw new RuntimeException("Unknown type");
    }
}
&lt;/pre&gt;

Ok, let's look at the code closely. Does your brain hurt and do your eyes
bleed already? No? Look again! They should! Please say loud to yourself: 'This
is bad code!'. Why? Because it's the prototype example of violating the Open
Closed Principle: You have some generalization (here Geometry) and make
assumptions about existing sub types. The overall idea of generalization is
well, generalize the fact that there are sub types. But why is it so awful
evil? Because it is not maintainable. What happens if you start adding new sub
types to Geometry? You would have to extend every single dispatching block and
add a new else if for your new type. And code which relies on this idiom does
not stop with a single block of such if else nonsense. No. It will spread
through the whole code. And I would bet a serious amount of money (if I would
have a serious amount of money) that you miss at least one place where you
have to add your new sub type.

&lt;p&gt;

The rescue to this problem is polymorphism. But we've already been at this
point. So we don't want polymorphism in the type hierarchy itself like our
draw() method. But what now? Well, please step aside for the Visitor pattern.
It lets you write specialized handling for each type, but prevents you from
implementing the type dispatching on your own. How? Have a look:

&lt;p&gt;

&lt;img src='http://yuml.me/diagram/scruffy;dir:td/class/%5BElement%5D%3C-%5BClient%5D,%20%5BVisitor%5D%3C-%5BClient%5D,%20%5BElement|accept%28Visitor%29%5D%5E%5BSubElement|accept%28Visitor%29%5D,%20%5BVisitor|visit%28SubElement%29%5D%5E%5BConcreteVisitor|visit%28SubElement%29%5D'/&gt;

&lt;p&gt;

The trick is SubElement's implementation of accept: 

&lt;pre name="code" class="java"&gt; 
public void accept(Visitor visitor){ 
    visitor.visit(this); 
} 
&lt;/pre&gt;

This is the magic polymorphic dispatching code! Let's see it in action: 
 
&lt;pre name="code" class="java"&gt;
Element elem = new SubElement();
elem.accept(new ConcreteVisitor());
&lt;/pre&gt;

Ok, not much action. But all we need for the explanation. We treat our
SubElement as a general Element. Calling the polymorphic accept actually on
SubElement will take care that the visit(SubElement) is finally executed. Hence
we did our dispatching, but without any if or even else. That's the power of
polymorphism!

&lt;p&gt;

Now let's go back to our Geometry world and see a more complex example:

&lt;p&gt;

&lt;img src='http://yuml.me/diagram/scruffy/class/%5BGeometry|accept%28Visitor%29%5D,%20%5BGeometry%5D%5E%5BLine|accept%28Visitor%29%5D,%20%5BGeometry%5D%5E%5BCircle|accept%28Visitor%29%5D,%20%5BGeometry%5D%5E%5BPolygon|accept%28Visitor%29%5D'/&gt;

&lt;img src='http://yuml.me/diagram/scruffy/class/%5BVisitor|visit%28Line%29;visit%28Circle%29;visit%28Polygon%29]^[DrawVisitor|visit%28Line%29;visit%28Circle%29;visit%28Polygon%29],'/&gt;

&lt;p&gt;

Here we have our DrawVisitor, who is responsible for drawing. The class
hierarchy just depends on Visitor and therefore knows nothing about our
concrete DrawVisitor. This goal has been reached. But, wait, we get even more,
for free. People usually advice you to use the Visitor on a stable class
hierarchy, hence on such, which does not frequently add new classes. Of course,
this is correct. Let's assume our classes would belong to a framework and
therefore we would not know anything about the client. if we constantly add
new classes to the model and change the interface people could become quite angry with us.

&lt;p&gt;

But (and that's a very huge but in my opinion) if you change your model, add
new classes and add the respective visit(NewClass) methods to the Visitor then
you get a compile time check whether you have taken care of handling the new
classes in all of your visitor's implementations. And having problems reported
by the compiler is one of the major advantages of a static type system, in my
honest opinion.

&lt;p&gt;

You might have noticed that Visitor visits all concrete classes, but not the
abstract super class. This is the necessary otherwise the whole dispatching
mechanism does not work. But on the other hand this introduces some challenges
on more complex type systems, where we have several levels of abstraction.
Even here we can use the visitor and build some handy tools with it to savely
navigate such hierarchies, but I think this might be an issue for another post.

&lt;p&gt;

Summarizing what we've learned today: The Visitor pattern enables us to
dispatch type hierarchies without violating the Open Closed principle by
injecting methods in classes and relying on good old runtime polymorphism.

&lt;p&gt;

Or do you have a complete different opinion on this topic?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-1090126559727808516?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/1090126559727808516/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=1090126559727808516' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/1090126559727808516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/1090126559727808516'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/06/hail-to-visitor-pattern.html' title='Hail to the Visitor Pattern'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-6457178063421476725</id><published>2009-02-22T14:52:00.010+01:00</published><updated>2009-02-22T17:19:35.425+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='polymorphism'/><category scheme='http://www.blogger.com/atom/ns#' term='dry'/><category scheme='http://www.blogger.com/atom/ns#' term='ant'/><title type='text'>Using ant to manage larger Java projects</title><content type='html'>&lt;p&gt;
In this post I'll give a brief introduction to my experience of managing larger
java projects with ant by sticking to the DRY principle.
&lt;p/&gt;

&lt;p&gt; 
One of the first steps during setting up a java project is surely the carefully
planning of the build system. A build system's purpose is doing the always
repeating tasks that occur during a project's life circle. The obvious ones are
creating temporary directories, calling the compiler, packaging everything to a
jar. If the complexity of the projects grows, it is quite certain that the
requirements to the build system grow as well. Surely we want to include unit
testing, javadoc generation. And what about automatic coverage analysis? Or
ordering a pizza? Yes. Surely it should be able to cope with these challenges
(maybe not the pizza thing, but i would love that...). There are roughly 2
approaches to get a build system in java: Either rely on the functionality your
favored IDE provides, or use one of widely accepted build systems like &lt;a
href="http://ant.apache.org/"&gt;ant&lt;/a&gt; or &lt;a
href="http://maven.apache.org/"&gt;maven&lt;/a&gt;. In my opinion there are a lot of
reasons not to choose the first one. Most importantly a particular project
should not depend on a certain IDE. Maybe there's a common agreement in your
company to focus on IDE, but this must not be the common case. As for every other
aspect of software development as well, you should try to avoid dependencies, or
least the unnecessary ones. On the other hand it is good to avoid complexity as
well. If your source code builds in a shell, then it will most likely build
every else. Hence, let's use some established build system like ant or maven.
&lt;/p&gt;
&lt;p&gt;
I am using ant for about eight years now, and I am quite happy with it. My
experiences are even that good, that I avoided the master the fully learning
curve towards maven up to now. But this post is not about ant and maven.
Actually I want to talk about how to maintain more complex projects. Let's start
small. In a common ant project you will most likely have a (very simplified)
structure like

&lt;pre&gt;
/root 
    /src 
    /build 
        /bin 
        /jars
    build.xml
&lt;/pre&gt;


Your source code lives in src, it gets compiled to build/bin and jared to
build/jars. You would have some targets, let's say init, compile, jar, which
create some dirs, compile the sources and, who would have guessed, pack them.
Anyways. But reality tends to be more complex than mickey mouse examples. If
your project is larger, it could be a good advice to break your code up
into different libraries. And even more important, with clear policies how to
put things into libraries. There are several aspects which would affect this
decision. Since your architecture is most likely organized in different
components, it is wise to reflect this structure in your source code too. You
should already have some policies for dependencies of your components, like 

&lt;ul&gt;
    &lt;li&gt;more specialized components have to depend on more generalized ones&lt;/li&gt;
    &lt;li&gt;don't rely on transitive dependencies&lt;/li&gt; 
    &lt;li&gt;permit cyclic dependencies&lt;/li&gt; 
&lt;/ul&gt; 

These rules are also valid on the actual library level.
&lt;p&gt;
But how should we reflect this in the directory layout? I've made good experiences
with the following approach: 

&lt;pre&gt;
/root
   /libs
     /commons
        /src
        /build
           ...
        build.xml
     /derived
        /src
        /build
        ...   
        build.xml
&lt;/pre&gt;

But now we are getting to the root of the matter: All build files will more or
less do the same. If we just copy them we would surely violate the DRY
principle, and that's something we should avoid. Let's define what we need: We
want to omit to redundantly specify the build process. Hence we want to have
something like a template, which we always use, but, and that's important, it
should leave enough freedom to make local modifications to this template. Why?
Let's assume we have such a template mechanism specified somewhere and we call
libraries with it. This is fine, as long as the libraries always have the same
requirements. But what if we have one particular lib which differs from these
requirements? let's say it consists of generated code, therefore it would have
to call a source code generator after init but before compile? we could put this
special target into the template as well and somehow ignore it as long as
there's nothing to generate. But in my opinion this violates OCP. It would be
better to provide some "hooks" when calling the template to do the special
things.
&lt;p&gt;
Luckily ant has some nice tool in its toolbox to overcome this problem: The &lt;a
href="http://ant.apache.org/manual/CoreTasks/import.html"&gt;import&lt;/a&gt; target.
This will let us specify some super build file, import its target and *tata*
override some targets (if we want to). Let's think about import as ant's way of
polymorphism. The super class (build file!) defines some abstract behaviour, the
base class individualizes it to its specific needs. Therefore we will define the
overall and general build steps in our template file and put wisely chosen hooks
into it, which could be overridden by the importing build file. A very simplified
super build file might look like

&lt;pre name="code" class="xml"&gt;
    &amp;lt;project name="Complex Ant Demo Master Libs"&gt;

        &amp;lt;!-- general library related properties --&gt;


        &amp;lt;property name="dir.src" location="${dir.library}/src"/&gt;
        &amp;lt;property name="dir.build" location="${dir.library}/build"/&gt;

        &amp;lt;property name="dir.bin" location="${dir.build}/bin"/&gt;
        &amp;lt;property name="dir.jars" location="${dir.build}/jars"/&gt;

        &amp;lt;property name="file.lib.jar" value="${dir.jars}/${name.library}.jar"/&gt;


        &amp;lt;target name="hook.init.pre"/&gt;
        &amp;lt;target name="hook.init.post"/&gt;

        &amp;lt;target name="init" description="Initializes the Library"&gt;

            &amp;lt;antcall target="hook.init.pre" /&gt;

            &amp;lt;mkdir dir="${dir.build}"/&gt;    
            &amp;lt;mkdir dir="${dir.bin}"/&gt;
            &amp;lt;mkdir dir="${dir.jars}"/&gt;

            &amp;lt;antcall target="hook.init.post" /&gt;

        &amp;lt;/target&gt;

        &amp;lt;target name="hook.compiler.pre"/&gt;
        &amp;lt;target name="hook.compile.post"/&gt;

        &amp;lt;target name="compile" description="Compiles Sources" depends="init"&gt;
            &amp;lt;antcall target="hook.compiler.pre"/&gt;

            &amp;lt;javac srcdir="${dir.src}" destdir="${dir.bin}"/&gt;

            &amp;lt;antcall target="hook.compile.post" /&gt;
        &amp;lt;/target&gt;

        &amp;lt;target name="hook.jar.pre"/&gt;
        &amp;lt;target name="hook.jar.post"/&gt;

        &amp;lt;target name="jar" description="Creates Jars" depends="compile"&gt;
            &amp;lt;antcall target="hook.jar.pre" /&gt;

            &amp;lt;jar basedir="${dir.build}" destfile="${file.lib.jar}"/&gt;

            &amp;lt;antcall target="hook.jar.post" /&gt;
        &amp;lt;/target&gt;    

        &amp;lt;target name="hook.clean.pre"/&gt;
        &amp;lt;target name="hook.clean.post"/&gt;

        &amp;lt;target name="clean" description="Cleans Up"&gt;

            &amp;lt;antcall target="hook.clean.pre"/&gt;

            &amp;lt;delete dir="${dir.build}" /&gt;

            &amp;lt;antcall target="hook.clean.post"/&gt;
        &amp;lt;/target&gt;

    &amp;lt;/project&amp;gt; 
&lt;/pre&gt;

That's a long piece of example. But let's go through focussing on the
interesting steps. First of all, we define some variables, which we will use
later. Please note, that we implicitly assume there are variables called
${dir.library} and ${name.library}. We will see later where they come from. It's
getting interesting in line 15. There we define those empty targets
hook.init.pre and hook.init.post. But there purpose becomes clear in the init
target below: First we call the (possible empty) pre hook, then we do our init
action, finally the post hook. This mechanism is used in each target. (to be
honest: I've seen this idea in the netbeans build system first...)

&lt;p&gt;
 
Let's assume we have library called "common", without any special treatment. Its 
build file is very simple now:

&lt;pre name="code" class="xml"&gt;
    &amp;lt;project name="Common Lib" basedir="."&gt;

        &amp;lt;property name="dir.library" location="${basedir}"/&gt;
        &amp;lt;property name="name.library" value="common"/&gt;
        &amp;lt;property name="dir.project-root" location="${basedir}/../.."/&gt;

        &amp;lt;import file="${dir.project-root}/build-master-lib.xml"/&gt;

    &amp;lt;/project&amp;gt; 
&lt;/pre&gt;
We simply specify those two implicit variables, just define the location for the
super build file and then import it. Now we can call all the imported targets on
common's build file. For instance &lt;pre&gt;ant jar&lt;/pre&gt; would result in the appropriate
jar file.

&lt;p&gt;
 
Finally consider our described specialized case. Do you remember? We want to
call the code generator before compile. That's how we do it:

&lt;pre name="code" class="xml"&gt;
    &amp;lt;project name="A Generated Lib" basedir="."&gt;

        &amp;lt;property name="dir.library" location="${basedir}"/&gt;
        &amp;lt;property name="name.library" value="generated"/&gt;
        &amp;lt;property name="dir.project-root" location="${basedir}/../.."/&gt;

        &amp;lt;import file="${dir.project-root}/build-master-lib.xml"/&gt;

        &amp;lt;property name="dir.gen" location="${dir.src}"/&gt;

        &amp;lt;!-- overrides target from build-master-lib.xml --&gt;
        &amp;lt;target name="hook.post.init"&gt;
            &amp;lt;mkdir dir="${dir.gen}"/&gt;
        &amp;lt;/target&gt;

        &amp;lt;target name="hook.compiler.pre"&gt;
            &amp;lt;echo&gt;Calling the source code generator&amp;lt;/echo&gt;
        &amp;lt;/target&gt;

        &amp;lt;target name="hook.clean.post"&gt;
            &amp;lt;mkdir dir="${dir.gen}"/&gt;
        &amp;lt;/target&gt;

    &amp;lt;/project&amp;gt;
&lt;/pre&gt;

We simply redefined the hooks to do all the housekeeping we need.

&lt;p&gt;
 
Of course this is only the first step in setting up a full blown, generically
managed ant project. But this should be enough for now.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-6457178063421476725?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/6457178063421476725/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=6457178063421476725' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/6457178063421476725'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/6457178063421476725'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2009/02/using-ant-to-manage-larger-java.html' title='Using ant to manage larger Java projects'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1509083967755245144.post-4463706939892047651</id><published>2008-08-06T21:59:00.001+02:00</published><updated>2009-02-21T15:06:56.897+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dependency injection'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><title type='text'>Select Spring Bean Implementations at Runtime</title><content type='html'>Finally I made myself familiar with Spring. I used the chance to integrate it into a new project I'm implementing at work. Although the basic idea of dependency injection is straigt forward, there are some concepts under the surface, which have to be discovered. &lt;p/&gt;

It seems to me, that the common spring application is something like a web app: the wiring between all the beans is done before staging and packaging the application into a war. Hence it is quite easy to write some spring bean definitions, give every bean a nice name, wire them properly together and that's that. &lt;/p&gt;

As a matter of fact I had to deal with a slightly different application paradigm. The user chooses at "startup time", which particular service realization he wants to use. Consider the following persistent service. There might be a realization that simply maintains the model in memory, on the other hand there could be realization that stores those model objects via hibernate into a database. Let's cut to the chase and give some handy code examples.

&lt;pre name="code" class="java"&gt;
// The Service
public interface MyService {

 void persist(Model model);
 
}

// Both realizations

public class InMemoryService implements MyService {

 @Override
 public void persist(Model model) {
  // store in memory...
 }
}

public class HibernateService implements MyService {

 @Override
 public void persist(Model model) {
  // store to hibernate
 }

}

// And finally the client using the service
public class Client {

 private MyService myService;
 
 public void setMyService(MyService myService) {
  this.myService = myService;
 }
 
 public void doSomeClientBusiness(){
  // whatever
 }
}
&lt;/pre&gt;

In the general spring paradigm you would write a bean definition containing all three beans and assign one of the two service realizations to client. Somehow like this

&lt;pre name="code"  class="xml"&gt;
&amp;lt;bean name="inMemoryService" class="foo.InMemoryService"/&amp;gt;

&amp;lt;bean name="hibernateService" class="foo.HibernateService" /&amp;gt;

&amp;lt;bean name="client" class="foo.Client"&amp;gt;
 &amp;lt;!--  choose the actual service --&amp;gt;
 &amp;lt;property name="myService" ref="hibernateService"/&amp;gt;
&amp;lt;/bean&amp;gt;
&lt;/pre&gt;

But that's not possible in our scenario, because
&lt;ol&gt;
&lt;li&gt;The client needs to decide which of both services to take when he starts the application
&lt;li&gt;The client may not be faced beans configs. That's not in the client's scope and would give him a too high degree of freedom.
&lt;/ol&gt;

So what can we do? We have to delay the instantiation of the service to the startup time depending on the user choice. So we need to detail what we mean by "user's choice". To keep it simple, let's just say the user could specify a value in a properties file like
&lt;pre name="code" class="java"&gt;
app.persist.service=IN_MEMORY
&lt;/pre&gt;

Now we need a mechanism that decides how to pick a particular realization depending on the parameter. Has anyone said factory? Yes, fine. Problem solved. The more ellaborated problem is to integrate this into the spring world. We need some bean we could define which finally yields our service realization. But spring comes with two mechanaism to the rescue. &lt;p/&gt;

First there is &lt;a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/FactoryBean.html"&gt;FactoryBean&lt;/a&gt;. If the spring container is faced with an implementation of a FactoryBean it does not instantiate the FactoryBean, but its product. With this we get something as follows. To distinguish which service to pick we define an enum:

&lt;pre name="code" class="java"&gt;
public enum ServiceType {
 IN_MEMORY, HIBERNATE
}
&lt;/pre&gt;

Well, this is a type safe way to maintain the instantiation, but surely this violates OCP. But let's just say that would be another discussion and ignore any consequences for now. 

Anyways, our factory looks like:

&lt;pre name="code" class="java"&gt;
public class MyServiceBean implements FactoryBean {

 private ServiceType serviceType;
 
 public void setServiceType(ServiceType serviceType) {
  this.serviceType = serviceType;
 }
 
 @Override
 public Object getObject() throws Exception {
  switch(serviceType) {
  case IN_MEMORY:
   return new InMemoryService();
  case HIBERNATE:
   return new HibernateService();
  default:
   throw new IllegalStateException();
  }
 }

 @Override
 public Class getObjectType() {
  return MyService.class;
 }

 @Override
 public boolean isSingleton() {
  return false;
 }
}
&lt;/pre&gt;

Nearly finished. The last thing is putting the runtime choosen parameter into the spring config. But how do we do that? Spring comes with a fine thing called &lt;a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html"&gt;PropertyPlaceholderConfigurer&lt;/a&gt;. This allows putting property placeholders into the bean definition, which are substituted by the respective value during the ApplicationContext's refresh. Now our bean definition looks like
&lt;pre name="code" class="xml"&gt;
&amp;lt;bean name="inMemoryService" class="foo.InMemoryService"/&amp;gt;

&amp;lt;bean name="hibernateService" class="foo.HibernateService" /&amp;gt;

&amp;lt;bean name="client" class="foo.Client"&amp;gt;
 &amp;lt;!--  choose the actual service --&amp;gt;
 &amp;lt;property name="myService" ref="${app.persist.service}"/&amp;gt;
&amp;lt;/bean&amp;gt;

&lt;/pre&gt;

And finally we need something like following ApplicationContext
&lt;pre name="code" class="xml"&gt;
String [] configs = {"my-services.xml"};
  ConfigurableApplicationContext appContext = 
   new ClassPathXmlApplicationContext(configs);
  
  PreferencesPlaceholderConfigurer placeholderConfigurer = 
   new PreferencesPlaceholderConfigurer();
  Properties props = new Properties();
  props.put("app.persist.service", "IN_MEMORY");
  placeholderConfigurer.setProperties(props);
  appContext.addBeanFactoryPostProcessor(placeholderConfigurer);
  appContext.refresh();
&lt;/pre&gt;

That's all.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1509083967755245144-4463706939892047651?l=42-incorrect-answers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://42-incorrect-answers.blogspot.com/feeds/4463706939892047651/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1509083967755245144&amp;postID=4463706939892047651' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/4463706939892047651'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1509083967755245144/posts/default/4463706939892047651'/><link rel='alternate' type='text/html' href='http://42-incorrect-answers.blogspot.com/2008/08/picking-service-realizations-at-runtime.html' title='Select Spring Bean Implementations at Runtime'/><author><name>Lars Girndt</name><uri>http://www.blogger.com/profile/09420488693074854446</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/-kjVlmxRLGh4/TzEMLuSXb-I/AAAAAAAAAGI/oPinULHzXto/s220/392155_314139661955116_100000772542278_832747_519434076_n.jpeg'/></author><thr:total>0</thr:total></entry></feed>
