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

<channel>
	<title>lexanderA &#187; Java</title>
	<atom:link href="http://lexandera.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://lexandera.com</link>
	<description>A blog about the web, mobile web, semantic web and mobile semantic web.</description>
	<lastBuildDate>Sun, 06 Jun 2010 18:27:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Reinventing the wheel: the fluent interface design pattern</title>
		<link>http://lexandera.com/2009/04/reinventing-the-wheel-the-fluent-interface-design-pattern/</link>
		<comments>http://lexandera.com/2009/04/reinventing-the-wheel-the-fluent-interface-design-pattern/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 15:56:31 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fluent interface]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[reinventing the wheel]]></category>
		<category><![CDATA[with]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=316</guid>
		<description><![CDATA[What&#8217;s the fluent interface design pattern? It&#8217;s a design pattern which became very visible in recent time, mainly due to jQuery, which was designed around it. In this pattern, class methods return the same object they were called on (they end with &#8220;return this;&#8221;), which makes it possible to chain them. It also makes it [...]]]></description>
			<content:encoded><![CDATA[<p>What&#8217;s the <a href="http://martinfowler.com/bliki/FluentInterface.html">fluent interface</a> design pattern? It&#8217;s a design pattern which became very visible in recent time, mainly due to <a class="zem_slink" title="JQuery" rel="homepage" href="http://jquery.com/">jQuery</a>, which was designed around it. In this pattern, class methods return the same object they were called on (they end with &#8220;return this;&#8221;), which makes it possible to chain them. It also makes it easily spottable in code, due to the distinctive outline created by chained methods.</p>
<p>Let&#8217;s have a quick look at what I&#8217;m talking about.</p>
<p>If Java Swing libraries supported this kind of coding, then the following block of java code, copied from <a href="http://asserttrue.blogspot.com/2009/04/swing-versus-death-by-paper-cut.html">a random blog post</a>, could be rewritten from this repetitive sequence:</p>
<pre name="code" class="java">

JFrame mainFrame = new JFrame(&quot;Hello World&quot;);
mainFrame.add(label, BorderLayout.CENTER);
mainFrame.addWindowListener(new MainFrameListener());
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
</pre>
<p>&#8230;to this chain of methods:</p>
<pre name="code" class="java">

new JFrame(&quot;Hello World&quot;)
    .add(label, BorderLayout.CENTER)
    .addWindowListener(new MainFrameListener())
    .setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
    .pack()
    .setLocationRelativeTo(null)
    .setVisible(true);
</pre>
<p>It&#8217;s amazing how much cleaner the code looks just by eliminating all those mainFrame references.</p>
<p>But wait&#8230; doesn&#8217;t this look familiar from somewhere? Doesn&#8217;t it remind you of a certain language feature you may have used years ago?</p>
<p>Yes, you&#8217;re right! It looks remarkably like the &#8220;with&#8221; control structure available in <a href="http://www.delphibasics.co.uk/RTL.asp?Name=With">Delphi/Pascal</a> and <a href="http://msdn.microsoft.com/en-us/library/wc500chb%28VS.71%29.aspx">VisualBasic</a>! If Java also supported it, the above example could be rewritten like this:</p>
<pre name="code" class="java">

with (new JFrame(&quot;Hello World&quot;)) {
    add(label, BorderLayout.CENTER);
    addWindowListener(new MainFrameListener());
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
}
</pre>
<p>It looks almost almost exactly the same as the one using the fluent interface, doesn&#8217;t it?</p>
<p>The main difference is that such a built-in construct works with any object you happen to throw at it, doesn&#8217;t require any additional effort from the library designer, and doesn&#8217;t require you to write repetitive code or wrapper classes if the library author failed to include the fluent interface pattern into the library in the first place. It also doesn&#8217;t interfere with the <a href="http://en.wikipedia.org/wiki/Command-query_separation">command-query separation principle</a> and is perfectly compatible with step through debuggers.</p>
<p>But the fluent interface pattern does make it possible for us to have a special name for a simple concept, a Wikipedia entry for it, and dozens of blog posts about a solution to a problem that&#8217;s been solved, in a better way, decades ago.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/pixy.gif?x-id=0d2ac358-b3a3-470d-b238-13d1e79d34b7"><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/04/reinventing-the-wheel-the-fluent-interface-design-pattern/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
