<?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>f3yourmind &#187; Patterns</title>
	<atom:link href="http://aslamkhan.net/tag/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://aslamkhan.net</link>
	<description>Ubuntu coding ... for your friends</description>
	<lastBuildDate>Mon, 24 Oct 2011 07:07:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Patterns at the September SPIN Event</title>
		<link>http://aslamkhan.net/software-development/patterns-at-the-september-spin-event/</link>
		<comments>http://aslamkhan.net/software-development/patterns-at-the-september-spin-event/#comments</comments>
		<pubDate>Sun, 19 Sep 2010 22:31:08 +0000</pubDate>
		<dc:creator>Aslam</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[SPIN]]></category>

		<guid isPermaLink="false">http://aslamkhan.net/?p=385</guid>
		<description><![CDATA[I suspect that many people did not understand what I meant about forces at play and that patterns describe a solution to bring some harmony among the forces in the problem in a particular context.  For the example of the airplane and wanting to serve the right meal to the right person, the challenge is [...]]]></description>
			<content:encoded><![CDATA[<p>I suspect that many people did not understand what I meant about forces at play and that patterns describe a solution to bring some harmony among the forces in the problem in a particular context.  For the example of the airplane and wanting to serve the right meal to the right person, the challenge is to serve the meal without knowing about the seating arrangement on the plane, and still sequentially access each seat.  Let&#8217;s look at how to get rid of the need to know the seating arrangement.</p>
<p>We start with the solution where we need to know the arrangement of seating and number of seats too.  BTW, it&#8217;s ruby code.</p>
<pre class="brush:ruby">for row in (0..29) do # 30 rows
    for pos in (0..5) do # seat A-F
       passenger = airplane.seats[row][pos]
       next if passenger.nil?
       passenger.serve_meal("nut free") if passenger.meal() == "nut free"
    end
end</pre>
<p>Of course, if we know the seat number, for example, 15C, then we can do this.  But that does not help at all.</p>
<pre class="brush:ruby">airplane.seats[14][2].serve_meal("nut free") if airplane.seats[14][2].meal() == "nut free"</pre>
<p>But, we still expose the data structure of the seats.  So, let&#8217;s make it a little better by using the iterator pattern on the data structure for the seats.</p>
<pre class="brush:ruby">airplane.seats.each do |row|
  row.each do | passenger |
    next if passenger.nil?
    passenger.serve_meal("nut free") if passenger.meal() == "nut free"
  end
end</pre>
<p>We could be cuter and do something like this and hide the seats array, but we still expose the numbering scheme of the seating.</p>
<pre class="brush:ruby">airplane.serve_meal("15C", "nut free")</pre>
<p>So, we can have the iterator on the seats data structure, which helps a bit.  But we can make it a lot better if we put an iterator on the airplane itself.  Now, we just care about occupied seats.</p>
<pre class="brush:ruby">airplane.each_occupied_seat do |passenger|
  passenger.serve_meal("nut free") if passenger.meal()
 == "nut free"
end</pre>
<p>In the airplane class, we have the following.</p>
<pre class="brush:ruby">class airplane
  ...
  def each_occupied_seat &amp;block
    @seats.each {|row| row.each { |pos| yield pos if not pos.nil?} }
  end
end</pre>
<p>We are using iterators on the encapsulated seats structure and exposing a new iterator for the airplane. Also, we are only work with seat that has a passenger</p>
<p>So, we started out with a deep need to know the structure, layout and limits of the seating in the airplane.  Then we started hiding the data structure for the seats, put iterators on this seats data structure which helped a bit.  But the real breakthrough happened when we started asking the airplane to just give us a way of sequentially accessing each seat that had a passenger.  No more conflicting forces, just a simple harmonious way to access each occupied seat on the plane.  And when the plane seating changes, we don&#8217;t care.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Faslamkhan.net%2Fsoftware-development%2Fpatterns-at-the-september-spin-event%2F&amp;title=Patterns%20at%20the%20September%20SPIN%20Event" id="wpa2a_2"><img src="http://aslamkhan.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://aslamkhan.net/software-development/patterns-at-the-september-spin-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Your ESB is going to kill you</title>
		<link>http://aslamkhan.net/software-development/your-esb-is-going-to-kill-you/</link>
		<comments>http://aslamkhan.net/software-development/your-esb-is-going-to-kill-you/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 20:15:20 +0000</pubDate>
		<dc:creator>Aslam</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[messaging]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://aslamkhan.net/?p=224</guid>
		<description><![CDATA[Recently I wrote about the fruitless plight of a schizophrenic service.  Now, I think that some of that schizophrenia exists in the ESB too (or is it rubbing off onto the ESB?).  I&#8217;ve always felt that the ESB was just another pattern that showed how to isolate things and deal with routing and transformations.  The [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I wrote about the fruitless plight of a <a href="http://aslamkhan.net/software-development/launching-the-services-support-group/">schizophrenic service</a>.  Now, I think that some of that schizophrenia exists in the ESB too (or is it rubbing off onto the ESB?).  I&#8217;ve always felt that the ESB was just another pattern that showed how to isolate things and deal with routing and transformations.  The most common implementation was a messaging gadget with some pluggable framework of sorts for the transformations, and some configurable framework for routing.</p>
<p>With such isolation of parts, it was convenient to not worry about what happened elsewhere when something was thrown to this gadget for processing.  And we started wondering about scalability things and decided that asynchronous was the way to go &#8230; disconnected, stateless, etc, all good, well-intentioned things and useful things.</p>
<p>Then the pattern became a product.  And on top of this product we had more products like business process orchestrators or workflow managers.  And below this product we had applications and databases and ftp locations and all sorts of things that catered for every imaginable protocol.  And around all of this we had some enterprise-ish sort of management thing to keep on eye on everything that was happening inside this very busy product.</p>
<p>Then, services moved from applications to the ESB product.  After all, it&#8217;s a <em>service</em> and that&#8217;s an enterprise <em>service</em> bus, right?  And when the services where moved over to their new home, all the dependencies had to come along too.  And then we started arguing about getting granularity right in the ESB.  I used to just think that the ESB had a proxy of sorts to the service that still was at the application.  Maybe I got it all wrong.</p>
<p>Now this ESB is starting to feel like an Application Server with a messaging gadget, workflow gadget, transformers, routers, protocol handlers.  And some ESB&#8217;s have a web server too, since they have browser based management consoles.</p>
<p>Some people also like the idea of a rules engine for their complex domain rules and embedded those in their applications.  Hold on, those content based routers in the ESB also used a rules engine.  Ok, let&#8217;s move our rules over to the ESB too.  Cool, my ESB is also a rules engine.</p>
<p>Now, I see people writing the most hellish XML that is meant to do everything from configure routing, define transformations, execute code, persist messages, fire off sets of rules and more.  It reminds me so much of those weird and wonderful stored procedures and cascading triggers that we wrote.  The other day I got a laugh out of a friend when I told him that ESB&#8217;s are now DB servers and everyone writes sprocs in XML.</p>
<p>And we tried to do everything in the database server &#8211; rules, custom types, defaults, constraints, sprocs, triggers, batch jobs &#8230; even jump into a shell and execute something else.  It did not work out very well then.</p>
<p>If I was an ESB, I&#8217;d be very confused.  I started life as a pattern with a reasonable implementation using messaging and transformation and routing.  Now all of this.  In fact, I&#8217;d be more stressed than confused.</p>
<p>Then again, maybe the ESB is not confused, and maybe the people that use the ESB that are confused.  In fact, if I was one of those people, I&#8217;d be stressed too.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Faslamkhan.net%2Fsoftware-development%2Fyour-esb-is-going-to-kill-you%2F&amp;title=Your%20ESB%20is%20going%20to%20kill%20you" id="wpa2a_4"><img src="http://aslamkhan.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://aslamkhan.net/software-development/your-esb-is-going-to-kill-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fast Track to Domain Driven Design</title>
		<link>http://aslamkhan.net/events/fast-track-to-domain-driven-design/</link>
		<comments>http://aslamkhan.net/events/fast-track-to-domain-driven-design/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 18:56:40 +0000</pubDate>
		<dc:creator>Aslam</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Ubiquitous Language]]></category>

		<guid isPermaLink="false">http://aslamkhan.net/?p=217</guid>
		<description><![CDATA[I finally got out of neutral and pulled together the first public offering our Domain Driven Design courses in Cape Town, South Africa.  Normally we give these courses on-site with people on the same development team but I thought it may be fun and inspiring to open it up to everyone for a change.  Now I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got out of neutral and pulled together the first public offering our <a href="http://www.factor10.com/courses/DDDfasttrack/capetown/about_this_course.html">Domain Driven Design courses</a> in Cape Town, South Africa.  Normally we give these courses on-site with people on the same development team but I thought it may be fun and inspiring to open it up to everyone for a change.  Now I&#8217;m all excited again and really looking forward to a diverse mixture of people. Hopefully, I will see some old faces and lots of new people.</p>
<p>The one thing I can tell you is that the course is a very immersive experience.  I really hate lecturing but I enjoy probing conversations and that&#8217;s how I give the course. I don&#8217;t have answers to the practical work and concerns are addressed as we go along.  As a result, the day takes unexpected turns and routes.  But in the end I get you to the right destination.  Come along; you will leave exhausted, but inspired!</p>
<h2><strong>Take the Fast Track to Domain Driven Design</strong></h2>
<p><a href="http://www.factor10.com/courses/DDDfasttrack/capetown/about_this_course.html">about the course</a> /  <a href="http://www.factor10.com/courses/DDDfasttrack/capetown/course_contents.html">course contents</a> / <a href="http://www.factor10.com/courses/DDDfasttrack/capetown/should_you_attend.html">should you attend?</a> / <a href="http://www.factor10.com/courses/DDDfasttrack/capetown/register.html">register for the course</a></p>
<p><a href="http://www.factor10.com">factor10</a> has expanded its services in South Africa to include our advanced and<br />
expert level courses aimed for the software professional.  On September 8-9, 2009,<br />
we will be offering a fast track to DDD for Architects at the BMW Pavilion in<br />
Cape Town.</p>
<p><img class="alignleft size-full wp-image-218" title="zz485d34cf" src="http://aslamkhan.net/wp-content/uploads/2009/08/zz485d34cf.jpg" alt="zz485d34cf" width="450" height="96" /></p>
<h2></h2>
<h2></h2>
<h2>Who should attend?</h2>
<p>This course is for software professionals that want to take the right steps towards<br />
advanced and expert levels in their careers.  <a href="http://www.factor10.com/courses/DDDfasttrack/capetown/register.html">Register</a> for this course if you want to &#8230;</p>
<ul>
<li>learn more than just another syntax and set of tools</li>
<li>write software for large, long living systems</li>
<li>increase the quality and maintainability of your design</li>
<li>design high quality models and use code to represent those models effectively</li>
<li>develop applications with a good APIs</li>
<li>add a design edge to your skill set</li>
</ul>
<p><img class="alignleft size-full wp-image-219" title="zz3caeb696" src="http://aslamkhan.net/wp-content/uploads/2009/08/zz3caeb696.jpg" alt="zz3caeb696" width="455" height="137" /></p>
<h2></h2>
<h2></h2>
<h2></h2>
<h2></h2>
<h2>Why should you learn DDD?</h2>
<p>More and more developers and architects realise that learning every detail of a new<br />
API just isn&#8217;t the way to deliver the best business value. It’s such a tough balancing<br />
act; focus on the solving the business problem and focus on building working software<br />
with your frameworks.</p>
<p>One way of taking a big leap in the right direction is to learn and apply domain driven<br />
design. It is definitely not abstract and fluffy; it deals a lot with the code also. DDD<br />
leads us to focus on understanding and to communicate that understanding very well;<br />
in language, in design and in code. You will shift your focus away from designing for a<br />
technology, and you will learn to design for the business domain; to the core of the<br />
problems and solutions. Those are the most interesting parts and what your users<br />
and customers really care about.</p>
<p><a href="http://www.factor10.com/courses/DDDfasttrack/capetown/about_this_course.html">about the course</a> /  <a href="http://www.factor10.com/courses/DDDfasttrack/capetown/course_contents.html">course contents</a> / <a href="http://www.factor10.com/courses/DDDfasttrack/capetown/should_you_attend.html">should you attend?</a> / <a href="http://www.factor10.com/courses/DDDfasttrack/capetown/register.html">register for the course</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Faslamkhan.net%2Fevents%2Ffast-track-to-domain-driven-design%2F&amp;title=Fast%20Track%20to%20Domain%20Driven%20Design" id="wpa2a_6"><img src="http://aslamkhan.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://aslamkhan.net/events/fast-track-to-domain-driven-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Øredev Presentations</title>
		<link>http://aslamkhan.net/events/oredev-presentations/</link>
		<comments>http://aslamkhan.net/events/oredev-presentations/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 21:30:43 +0000</pubDate>
		<dc:creator>Aslam</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[aspects]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[factor10]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[oredev]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://aslamkhan.net/?p=62</guid>
		<description><![CDATA[My presentations from Oredev are finally available.  After working through almost all the export options on Keynote, I have settled on QuickTime as the distro format.  The &#8220;flying code&#8221; in the aspects presentation worked out best with QuickTime.  Note that it&#8217;s not a continuous playback and you have to click-through each frame. Solving Domain Problems [...]]]></description>
			<content:encoded><![CDATA[<p>My presentations from Oredev are finally available.  After working through almost all the export options on Keynote, I have settled on QuickTime as the distro format.  The &#8220;flying code&#8221; in the aspects presentation worked out best with QuickTime.  Note that it&#8217;s not a continuous playback and you have to click-through each frame.</p>
<ul>
<li><strong>Solving Domain Problems with Aspects </strong>has a couple of slides with repeated transitions (courtesy of the export <img src='http://aslamkhan.net/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' />  ).  This one has the <em>flying code</em>, as <a title="Claudio Perrone's Blog" href="http://www.claudioperrone.com">Claudio Perrone</a> calls it!  And it is the presentation that lead to the chat with <a href="http://ayende.com">Ayende Rahien</a>.  He has done something similar in C# <a href="http://ayende.com/Blog/archive/2008/11/23/aspects-of-domain-design.aspx">here</a>. <span style="color: #808080;">- 29.1MB @ <span style="color: #999999;"><a href="http://aslamkhan.net/wp-content/uploads/2008/11/SolvingDomainProblemsWithAspects.mov">http://aslamkhan.net/wp-content/uploads/2008/11/SolvingDomainProblemsWithAspects.mov</a></span> </span></li>
</ul>
<ul>
<li><strong>Managing Diversity in Agile Teams</strong> was inspired by Claudio&#8217;s presentation style.  Highly visual with minimal text.  It&#8217;s about story telling, movie script style and not about bullet point presentations.  Thanks, Claudio! <span style="color: #808080;">- 16.6MB @ </span><span style="color: #808080;"><span style="color: #999999;"><a href="http://aslamkhan.net/wp-content/uploads/2008/11/ManagingDiversityInAgileTeams.mov">http://aslamkhan.net/wp-content/uploads/2008/11/ManagingDiversityInAgileTeams.mov</a></span></span><a href="http://aslamkhan.net/wp-content/uploads/2008/11/ManagingDiversityInAgileTeams.mov"></a></li>
</ul>
<ul>
<li><strong>Bootstrapping your SOA Project</strong> has the slides for the workshop that I ran.  It&#8217;s a mixture of traditional and visual.  The traditional is used purely for reference / take-home material. <span style="color: #808080;">- 13.5MB @ </span><span style="color: #808080;"><span style="color: #999999;"><a href="http://aslamkhan.net/wp-content/uploads/2008/11/BootstrappingYourSOAProject.mov">http://aslamkhan.net/wp-content/uploads/2008/11/BootstrappingYourSOAProject.mov</a></span></span></li>
</ul>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Faslamkhan.net%2Fevents%2Foredev-presentations%2F&amp;title=%C3%98redev%20Presentations" id="wpa2a_8"><img src="http://aslamkhan.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://aslamkhan.net/events/oredev-presentations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://aslamkhan.net/wp-content/uploads/2008/11/SolvingDomainProblemsWithAspects.mov" length="30585840" type="video/quicktime" />
<enclosure url="http://aslamkhan.net/wp-content/uploads/2008/11/ManagingDiversityInAgileTeams.mov" length="17409202" type="video/quicktime" />
<enclosure url="http://aslamkhan.net/wp-content/uploads/2008/11/BootstrappingYourSOAProject.mov" length="14184983" type="video/quicktime" />
		</item>
		<item>
		<title>Factories, Builders and Fluent Interfaces</title>
		<link>http://aslamkhan.net/software-development/factories-builders-and-fluent-interfaces/</link>
		<comments>http://aslamkhan.net/software-development/factories-builders-and-fluent-interfaces/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 21:27:05 +0000</pubDate>
		<dc:creator>Aslam</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://aslamkhan.net/?p=45</guid>
		<description><![CDATA[Last week I started working on very short proof of concept with a team that I am currently coaching at a short term insurance company.  We hit a very common design decision: when do we use a factory pattern and when do we use a builder pattern. In the problem at hand, we needed to [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I started working on very short proof of concept with a team that I am currently coaching at a short term insurance company.  We hit a very common design decision: when do we use a factory pattern and when do we use a builder pattern.</p>
<p>In the problem at hand, we needed to describe an item that will appear in an insurance policy that must be covered for fire damage.  It turns out that these items are not trivial in their structure, and many things influence the premium that will paid by the policy holder for fire insurance.  So, the first bit of code (in Java) in the test looked something like this.</p>
<pre><span style="font-family: 'Courier New'; white-space: pre;">FireItem fireItem = new FireItem();
fireItem.setIndustry("Building Construction");
fireItem.setOccupationCode("Workshop");
// the postal/zip code for the location of the insured item
fireItem.setAreaCode(7800);
// ignore the number, we actually created a Money class
fireItem.setSumInsured(200000.00);
fireItem.setRoofing("Non-Standard");</span></pre>
<p> </p>
<p>After the test passed, we refactored and I sneaked in a factory method which will be used to honor default values and at the same time threw in a <a href="http://www.martinfowler.com/bliki/FluentInterface.html">fluent interface</a> (the term coined by Eric Evans and Martin Fowler.  After all, I was also quietly introducing Domain Driven Design without actually saying that).</p>
<p>The code looked like this.</p>
<pre><span style="font-family: 'Courier New'; white-space: pre;">FireItem fireItem = FireItem.create()
                            .inIndustry("Building Construction")
                            .withOccupation("Workshop")
                            .InAreaWithPostalCode(7800)
                            .forSumInsured(200000.00)
                            .havingRoofing("Non-Standard");
</span></pre>
<p>The FireItem class looked like this:</p>
<pre><span style="font-family: 'Courier New'; white-space: pre;">public class FireItem {
    String industry;
    String occupation;
    // other properties ...

   private FireItem() { }
   public static FireItem create() {
       return new FireItem();
   }
   public FireItem inIndustry(String industry) {
      this.industry = industry;
      return this;
   }
   // other chained methods follow a similar style returning "this" ...
}
</span></pre>
<p>Nice! Much more readable. But, we then realised that it&#8217;s easy for someone to miss one of the methods in the chain.  That will result in the item having an incomplete structure.  Not good!  </p>
<p>One of the things I tend to do as a coach, is to let the team I am working with, experience the problem, solution and any rewards and smells as well.  Sometimes I even throw in red herring for sake of experience <img src='http://aslamkhan.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .  So, the third pass at refactoring was to introduce a validate() method on the item which throws an exception if everything was not in place.</p>
<pre><span style="font-family: 'Courier New'; white-space: pre;">try {
  FireItem fireItem = FireItem.create()
                              .inIndustry("Building Construction")
                              .withOccupation("Workshop")
                              .InAreaWithPostalCode(7800)
                              .forSumInsured(200000.00)
                              .havingRoofing("Non-Standard")
                              .validate();
} catch (FireItemException e) {
  // handle the exception
}
</span></pre>
<p>Now the user of this class needs to know that the validate() method must be called before they really want to use an item object.  Yuck, that&#8217;s smelly!  So, for the fourth design refactoring, I introduced a builder and moved the fluent interface to the builder, still using method chaining but introduced a build() method that did the work of the previous validate() method before returning the well structured item.  The FireItem class now needs the traditional bunch of getters and setters (rant &#8211; the framework goodies need them anyway!!)</p>
<pre><span style="font-family: 'Courier New'; white-space: pre;">import static insurance.FireItemBuilder.fireItem;
// ...
try {
  FireItem fireItem = fireItem().inIndustry("Building Construction")
                                .withOccupation("Workshop")
                                .InAreaWithPostalCode(7800)
                                .forSumInsured(200000)
                                .havingRoofing("Non-Standard")
                                .build();
} catch (FireItemException e) {
   // handle the exception
}
</span></pre>
<p>Much better!  Note the use of the static import which gives us the liberty to use the static method without specifying the class in code body.  The FireItemBuilder class looked like this.</p>
<pre><span style="font-family: 'Courier New'; white-space: pre;">public class FireItemBuilder {
   private final FireItem fireItem;
   private FireItemBuilder() { 
      fireItem = new FireItem();
   }
   public static FireItemBuilder fireItem() {
       return new FireItemBuilder();
   }
   public FireItemBuilder inIndustry(String industry) {
      fireItem.setIndustry(industry);
      return this;
   }
   // other chained methods follow a similar style returning "this" ...
   public FireItem build() throws FireItemBuilderException {
      validate();
      return fireItem;
   }
   private void validate() throws FireItemBuilderException {
     // do all validations on the fire item itself and throw an exception if something fails
   }
}
</span></pre>
<p>Sure, we can improve the bubbling of the exception from validate() to build() and we could do with a better name for validate().  And perhaps, validate() should be on the FireItem class.  But let&#8217;s stick to factories and builders and fluent interfaces.  I think these three things work nicely &#8220;together&#8221;, when used for the right purpose.</p>
<p>In a nutshell, factories are great for creating objects where defaults and invariants are easily honored during the simple call to the factory.  However, if the structure of the object is more complex which makes long argument lists ugly, and some form of validation is necessary  before we can use an object then a builder works beautifully.</p>
<p>Also, note that the fluent interface was used to improve readability and kick off a tiny little DSL for describing insurance items.</p>
<p>An alternative is to allow the object to have an invalid structure but you track it with an invalid state, perhaps using a state pattern.  This is not exactly what the state pattern was meant for, but it will work nonetheless.</p>
<p>The last time I was with this team was in August 2006, and it is really great to work with them again.  So, much more for me to learn from them.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Faslamkhan.net%2Fsoftware-development%2Ffactories-builders-and-fluent-interfaces%2F&amp;title=Factories%2C%20Builders%20and%20Fluent%20Interfaces" id="wpa2a_10"><img src="http://aslamkhan.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://aslamkhan.net/software-development/factories-builders-and-fluent-interfaces/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Looking after your domain model</title>
		<link>http://aslamkhan.net/software-development/looking-after-your-domain-model/</link>
		<comments>http://aslamkhan.net/software-development/looking-after-your-domain-model/#comments</comments>
		<pubDate>Thu, 03 Jan 2008 20:02:01 +0000</pubDate>
		<dc:creator>Aslam</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://aslamkhan.net/software-development/looking-after-your-domain-model/</guid>
		<description><![CDATA[Mats Helander has written an excellent article on how to manage your domain model with some intelligent design trade-offs.  It&#8217;s a lengthy article that even manages to introduce AOP as well.  If you start reading it and wonder where it&#8217;s going, just carry on reading&#8230;it is written in an evolutionary style.  Nice article, Mats! UPDATE: I have written the Java [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.matshelander.com/wordpress/">Mats Helander</a> has written an excellent <a href="http://www.infoq.com/articles/aspects-of-domain-model-mgmt">article</a> on how to manage your domain model with some intelligent design trade-offs.  It&#8217;s a lengthy article that even manages to introduce AOP as well.  If you start reading it and wonder where it&#8217;s going, just carry on reading&#8230;it is written in an evolutionary style.  Nice article, Mats! <span style="font-weight: bold" class="Apple-style-span">UPDATE: </span>I have written the Java equivalent of the listing in Mats&#8217; article and <a href="http://aslamkhan.net/wp-content/uploads/2008/01/mats-dmm.zip" title="mats-dmm.zip">attached</a> it.  The AOP part uses <a href="http://www.springframework.org">SpringFramework</a> 2.5 and AspectJ.  </p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Faslamkhan.net%2Fsoftware-development%2Flooking-after-your-domain-model%2F&amp;title=Looking%20after%20your%20domain%20model" id="wpa2a_12"><img src="http://aslamkhan.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://aslamkhan.net/software-development/looking-after-your-domain-model/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

