<?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>Matthew Wells</title>
	<atom:link href="http://matthewwells.org.uk/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewwells.org.uk/blog</link>
	<description>Java, J2EE &#38; Configurable Systems</description>
	<lastBuildDate>Wed, 23 Mar 2011 22:29:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>One Minute Patterns GOF State</title>
		<link>http://matthewwells.org.uk/blog/2011/03/one-minute-patterns-gof-state/</link>
		<comments>http://matthewwells.org.uk/blog/2011/03/one-minute-patterns-gof-state/#comments</comments>
		<pubDate>Wed, 23 Mar 2011 22:29:09 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[GOF]]></category>
		<category><![CDATA[One Minute]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://matthewwells.org.uk/blog/?p=25</guid>
		<description><![CDATA[Here&#8217;s the somewhat maligned GOF State Pattern. This allows us to localise state specific behaviour into a single subclass for each state. The State interface defines the behaviour that each concrete State class must implement. Here&#8217;s a simple example. (We&#8217;re all familiar with traffic lights!) That&#8217;s it! What a state! Got more than one minute? [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the somewhat maligned GOF State Pattern.  This allows us to localise state specific behaviour into a single subclass for each state. The State interface defines the behaviour that each concrete State class must implement. Here&#8217;s a simple example. (We&#8217;re all familiar with traffic lights!)<br />
<a href="http://matthewwells.org.uk/blog/2011/03/one-minute-patterns-gof-state/state-pattern-2/" rel="attachment wp-att-43"><img src="http://matthewwells.org.uk/blog/wp-content/uploads/2011/03/State-Pattern1.png" alt="State Pattern Diagram" title="State Pattern" width="567" height="387" class="alignnone size-full wp-image-43" /></a><br />
That&#8217;s it! What a state!<br />
Got more than one minute? <span id="more-25"></span>OK, lets go into a bit more detail. The context object only ever references a single concrete State object to which it delegates state specific activity.  This ensures that state transitions are atomic.  The transitions between states can be managed by the Context, or (my preference) this control can be delegated to the State objects themselves.</p>
<p>How about some code.<br />
Here&#8217;s the State interface.</p>
<pre class="code">
public interface TrafficLightState {
  public TrafficLightState transition();
  public Light[] illuminate();
}
</pre>
<p>Here&#8217;s the an example of a concrete State class, representing the Stop state. See how it defines the following state in its transition method. The illuminate method defines the state specific behaviour, in this case which lights should be turned on.</p>
<pre class="code">
public class StopState implements TrafficLightState {

  public TrafficLightState transition()  {
    return new ReadyGoState();
  }

  public Light[] illuminate() {
      return new Light[]{Light.red, Light.greenWalkingMan};
  }
}
</pre>
<p>And here&#8217;s the context class.</p>
<pre class="code">
public class TrafficLightContext {
  private TrafficLightState state;

  public TrafficLightContext()  {
    state = new StopState();
  }

  public void run()  {
    for (;;)  {
    try{
      for (Light light : state.illuminate()){
        System.out.print(light.name()+" ");
      }
      Thread.sleep(2000);
      state = state.transition();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    }
  }

  public static void main(String[] args)  {
    TrafficLightContext context = new TrafficLightContext();
    context.run();
  }
}
</pre>
<p>Alternatively, the State objects could be passed a reference to the Context object so that they could access contextual data and modify the Context&#8217;s State at whatever point is appropriate.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewwells.org.uk/blog/2011/03/one-minute-patterns-gof-state/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One Minute Patterns GOF Decorator</title>
		<link>http://matthewwells.org.uk/blog/2008/07/one-minute-patterns-gof-decorator/</link>
		<comments>http://matthewwells.org.uk/blog/2008/07/one-minute-patterns-gof-decorator/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 11:54:20 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[GOF]]></category>
		<category><![CDATA[One Minute]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.naturalobjects.co.uk/blog/2008/07/07/one-minute-patterns-gof-decorator/</guid>
		<description><![CDATA[OK, so lets start off with the GOF Decorator Pattern which allows us to extend functionality at runtime, without using inheritance. The Decorator object wraps the instance whose functionality it&#8217;s enhancing, but implements the same interface as the original object. It delegates calls to the wrapped object, but has the opportunity to augment the behaviour [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so lets start off with the GOF Decorator Pattern which allows us to extend functionality at runtime, without using inheritance.  The Decorator object wraps the instance whose functionality it&#8217;s enhancing, but implements the same interface as the original object.  It delegates calls to the wrapped object, but has the opportunity to augment the behaviour of the method before and/or after the delegated call. Here&#8217;s a very simple example.<br />
<img src="http://www.naturalobjects.co.uk/blog/wp-content/assets/patterns/Decorator%20Pattern%20-%20Simple.png" alt="GOF Decorator Pattern (Simple)" vspace="10" hspace="70" /><br />
That&#8217;s it! A Decorator.  You may have used it already without realising!<br />
Got more than one minute? <span id="more-11"></span>OK, lets go into a bit more detail.  We may well want to implement a number of different decorators, which we can combine in different ways at runtime. (This will be much more flexible than if we&#8217;d used inheritance.)  Now we can add an abstract decorator base class which can handle the wrapped instance and the delegation (which all our decorators will have in common).Let&#8217;s decorate a Christmas Tree!<img src="http://www.naturalobjects.co.uk/blog/wp-content/assets/patterns/One%20Minute%20Patterns%20-%20Decorator%20Pattern.png" alt="GOF - Decorator Pattern" vspace="10" width="580" height="415" /></p>
<p>The Decorators aren&#8217;t actually Christmas Trees themselves.  In fact they are useless without a real tree to wrap themselves around.  They impersonate a Tree by implementing the interface, but delegate all of the real work of being a tree to the wrapped instance.  How about some code.<br />
Here&#8217;s the abstract Decorator base class.</p>
<pre class="code">
public abstract class ChristmasTreeDecorator implements ChristmasTree {
  private ChristmasTree wrappedChristmasTree;

  public ChristmasTreeDecorator(ChristmasTree tree) {
    wrappedChristmasTree = tree;
  }

  protected ChristmasTree getTree() {
    return wrappedChristmasTree;
  }
}</pre>
<p>There&#8217;s a great opportunity here to use another GOF pattern &#8211; Template Method, but I&#8217;ve resisted the temptation to keep things simple.  If you have time, have a look yourself at how it might fit in.</p>
<p>Here&#8217;s the an example of a concrete Decorator class.</p>
<pre class="code">
public class FairyDecorator extends ChristmasTreeDecorator {
  public FairyDecorator(ChristmasTree tree){
    super(tree);
  }

  public void cheerRoom() {
    // could add behaviour before delegated call too if we want...
    getTree().cheerRoom();
    addFairy();
  }

  private void addFairy() {
    System.out.println("Top off with a festive fairy!");
  }
}</pre>
<p>And here&#8217;s some test code.</p>
<pre class="code">
public class DecoratorTest {
  public static void main(String[] args) {
    System.out.println("Here's my new tree, let's see how it cheers the room...");
    ChristmasTree tree = new NorwaySpruce();
    tree.cheerRoom();

    System.out.println("\nA bit dull, lets add some lights...");
    tree = new LightsDecorator(tree);
    tree.cheerRoom();

    System.out.println("\nNot bad, how about some tinsel...");
    tree = new TinselDecorator(tree);
    tree.cheerRoom();

    System.out.println("\nAnd now the finishing touch, a fairy...");
    tree = new FairyDecorator(tree);
    tree.cheerRoom();
  }
}</pre>
<p>Notice how Decorators can be nested. A good example of this pattern in the Java APIs is the I/O Stream classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewwells.org.uk/blog/2008/07/one-minute-patterns-gof-decorator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One Minute Patterns Introduction</title>
		<link>http://matthewwells.org.uk/blog/2008/07/one-minute-patterns-introduction/</link>
		<comments>http://matthewwells.org.uk/blog/2008/07/one-minute-patterns-introduction/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 19:33:08 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[One Minute]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.naturalobjects.co.uk/blog/2008/07/04/one-minute-patterns-introduction/</guid>
		<description><![CDATA[I was recently studying patterns for the Sun Certified Enterprise Architect for Java EE 5 exam and I realised that I was really only familiar with a small proportion of them.  I&#8217;d owned the seminal GOF Design Patterns book for 8 years and the Core J2EE Patterns book for five years, but I was still [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently studying patterns for the Sun Certified Enterprise Architect for Java EE 5 exam and I realised that I was really only familiar with a small proportion of them.  I&#8217;d owned the seminal GOF <a href="http://www.amazon.co.uk/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.co.uk%2FDesign-patterns-elements-reusable-object-oriented%2Fdp%2F0201633612%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1215200384%26sr%3D8-2&amp;tag=natobjltd-21&amp;linkCode=ur2&amp;camp=1634&amp;creative=6738">Design Patterns</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=natobjltd-21&amp;l=ur2&amp;o=2" style="border: medium none ! important; margin: 0px ! important" border="0" width="1" height="1" /> book for 8 years and the <a href="http://www.amazon.co.uk/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.co.uk%2FCore-J2EE-Patterns-Practices-Strategies%2Fdp%2F0131422464%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1215200709%26sr%3D8-18&amp;tag=natobjltd-21&amp;linkCode=ur2&amp;camp=1634&amp;creative=6738">Core J2EE Patterns</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=natobjltd-21&amp;l=ur2&amp;o=2" style="border: medium none ! important; margin: 0px ! important" border="0" width="1" height="1" /> book for five years, but I was still only really familiar with the most commonly referenced patterns.</p>
<p>What interested me even more was that once I started to study them, many of them represent very simple concepts that I&#8217;d been using in my designs for years.  Having studied the approach of the <a href="http://www.amazon.co.uk/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.co.uk%2FHead-First-Design-Patterns%2Fdp%2F0596007124%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1215200384%26sr%3D8-1&amp;tag=natobjltd-21&amp;linkCode=ur2&amp;camp=1634&amp;creative=6738">Head First Design Patterns</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=natobjltd-21&amp;l=ur2&amp;o=2" style="border: medium none ! important; margin: 0px ! important" border="0" width="1" height="1" /> book (highly recommended!), I wondered whether it might be possible to write short, self describing examples of each pattern that could get the fundamental concept across in a quick and memorable way.  I am going to give it a go and post them onto this blog. If they help you to grasp the concept of even one new pattern then I&#8217;d love to hear from you!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewwells.org.uk/blog/2008/07/one-minute-patterns-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Conditional Compilation in Java &#8211; Almost</title>
		<link>http://matthewwells.org.uk/blog/2008/02/conditional-compilation-in-java-almost/</link>
		<comments>http://matthewwells.org.uk/blog/2008/02/conditional-compilation-in-java-almost/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 23:18:18 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Process]]></category>
		<category><![CDATA[ant]]></category>

		<guid isPermaLink="false">http://www.naturalobjects.co.uk/blog/2008/02/28/conditional-compilation-in-java-almost/</guid>
		<description><![CDATA[Being a little bit long in the tooth and originally coming from an embedded C background, I occasionally hanker after the flexibility of conditional compilation. This is a C based mechanism that allows you to build different flavours of a product from the same code base at compile time by a combination of #define and [...]]]></description>
			<content:encoded><![CDATA[<p>Being a little bit long in the tooth and originally coming from an embedded C background, I occasionally hanker after the flexibility of conditional compilation.</p>
<blockquote><p>This is a C based mechanism that allows you to build different flavours of a product from the same code base at compile time by a combination of <code>#define</code> and <code>#ifdef</code> primitives and a pre-processing step which occurred before compilation.  As the name suggests, this process enabled blocks of code to be included in the final executable (or not) dependent on predefined conditions.</p></blockquote>
<p>I&#8217;ve dabbled with various ways of achieving a similar effect in Java and so far this is the simplest approach that I&#8217;ve come up with.  Let me know if you have a better strategy.  The scheme involves simply running a single ant task prior to invoking the Java compiler which effectively selects the product flavour that you intend to build.<br />
Firstly, define all of the different flavours of product that you wish to build in a class <code>ProductType.java</code>. For example:</p>
<ol class="code">
<li class="t0">public class ProductType  {</li>
<li class="t1">public static final String BANANAS = &#8220;bananas&#8221;;</li>
<li class="t1">public static final String APPLES = &#8220;apples&#8221;;</li>
<li class="t1">public static boolean isBananas() {</li>
<li class="t2">return SetProduct.THIS_PRODUCT.equals(BANANAS);</li>
<li class="t1">}</li>
<li class="t1">public static boolean isApples() {</li>
<li class="t2">return SetProduct.THIS_PRODUCT.equals(APPLES);</li>
<li class="t1">}</li>
<li class="t0">}</li>
</ol>
<p>Next, somewhere outside your source code tree, create a product directory for each of the product flavours you need to support.  Each of these directories will hold its own version of the <code>SetProduct.java</code> class, which takes the following form:</p>
<ol class="code">
<li class="t0">public final class SetProduct  {</li>
<li class="t1">public static final String THIS_PRODUCT = ProductType.BANANAS;</li>
<li class="t0">}</li>
</ol>
<p>The ant task for each product flavour copies the <code>SetProduct.java</code> class from the specified product directory into the correct location in your java source code tree.</p>
<ol class="code">
<li class="t0">&lt;target name=&#8221;bananas&#8221;&gt;</li>
<li class="t1">&lt;copy overwrite=&#8221;true&#8221; todir=&#8221;src/mypackage&#8221;&gt;</li>
<li class="t2">&lt;fileset dir=&#8221;products/bananas&#8221;&gt;</li>
<li class="t3">&lt;include name=&#8221;SetProduct.java&#8221;&gt;</li>
<li class="t2">&lt;/fileset&gt;</li>
<li class="t1">&lt;/copy&gt;</li>
<li class="t0">&lt;/target&gt;</li>
</ol>
<p>It is important that no version of this class exists in the source code tree within your version control system.  This ensures that a product flavour must be selected via an ant task before the code can be compiled successfully.  (Other non-java files, such as web.xml which vary between product flavours can also be copied across at this point by the same ant task, but the <code>SetProduct.java</code> file is the only one that is required to support conditional compilation.)</p>
<p>Within your code base you can now define conditional blocks in the following way:</p>
<ol class="code">
<li class="t1">if (ProductType.isBananas()){</li>
<li class="t2">yellow = true;</li>
<li class="t2">peelable = true;</li>
<li class="t1">}</li>
<li class="t1">else if (ProductType.isApples()){</li>
<li class="t2">yellow = false;</li>
<li class="t2">peelable = true;</li>
<li class="t1">}</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://matthewwells.org.uk/blog/2008/02/conditional-compilation-in-java-almost/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Desert Island Downloads</title>
		<link>http://matthewwells.org.uk/blog/2007/12/desert-island-downloads/</link>
		<comments>http://matthewwells.org.uk/blog/2007/12/desert-island-downloads/#comments</comments>
		<pubDate>Mon, 17 Dec 2007 21:43:46 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[XStream]]></category>

		<guid isPermaLink="false">http://www.fantasyedge.co.uk/blog/2007/12/17/desert-island-downloads/</guid>
		<description><![CDATA[OK, so you&#8217;re washed up on a desert island and not a broadband connection in sight. Assuming you have the know-how to craft a half decent Java platform out of the flotsam and jetsam on the beach, what are the downloads that you always carry on your flash drive for just such an occasion? I&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so you&#8217;re washed up on a desert island and not a broadband connection in sight.  Assuming you have the know-how to craft a half decent Java platform out of the flotsam and jetsam on the beach, what are the downloads that you always carry on your flash drive for just such an occasion?</p>
<p>I&#8217;ll be a bit more specific.  As Java&#8217;s my thing, I&#8217;m thinking of those open source gems that I really would not want to have to code again from scratch, even if I could, under any circumstances (although I can think of worse circumstances than under a coconut palm on a beach!)</p>
<p>Right at the top of my list, I would have to put <a href="http://xstream.codehaus.org/">xstream</a>. In their own words:</p>
<blockquote><p>XStream is a simple library to serialize objects to XML and back again.</p></blockquote>
<p>I&#8217;ve been using this library for about 3 years, mainly to support the reading, writing and upgrading of configuration files for configurable systems.  I initially used it to enable me to add configurable functionality into a prototype system very rapidly, but it was so effective that I saw no reason to change the approach in the production system.  If you are taking an object-centric approach to XML then I can see no reason to use anything else.</p>
<p>In the early days the emphasis was on ease of use (which was and still is stunning!), but subsequent releases have provided much more flexibility in the way that objects graphs can be mapped to XML schemas.  I&#8217;m still a big fan and the library has become an essential part of my toolkit for implementing configurable systems.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewwells.org.uk/blog/2007/12/desert-island-downloads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Standing on the Shoulders of Giants</title>
		<link>http://matthewwells.org.uk/blog/2007/12/standing-on-the-shoulders-of-giants/</link>
		<comments>http://matthewwells.org.uk/blog/2007/12/standing-on-the-shoulders-of-giants/#comments</comments>
		<pubDate>Sat, 15 Dec 2007 23:17:53 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.fantasyedge.co.uk/blog/?p=3</guid>
		<description><![CDATA[Standing on the shoulders of giants is something that it is impossible not to do in the Software industry. There are many giants on whose shoulders I am very much aware of standing. Rumbaugh, Booch &#38; Jacobson; the Gang-of-Four, Richard Stallman, Linus Torvalds, Kent Beck and Martin Fowler are all names that spring easily to [...]]]></description>
			<content:encoded><![CDATA[<p>Standing on the shoulders of giants is something that it is impossible not to do in the Software industry.  There are many giants on whose shoulders I am very much aware of standing. <a href="http://en.wikipedia.org/wiki/Unified_Modeling_Language">Rumbaugh, Booch &amp; Jacobson</a>; the <a href="http://en.wikipedia.org/wiki/Design_Patterns">Gang-of-Four</a>, <a href="http://www.stallman.org/">Richard Stallman</a>, <a href="http://en.wikipedia.org/wiki/Linus_Torvalds">Linus Torvalds</a>, <a href="http://c2.com/cgi/wiki?KentBeck">Kent Beck</a> and <a href="http://www.martinfowler.com">Martin Fowler</a> are all names that spring easily to my mind.</p>
<p>However the individuals that I&#8217;m posting about, whilst maybe not quite giants are certainly taller than me in their respective sphere and standing on their shoulders has improved my view!  I&#8217;m actually talking about the rather pedestrian (to some!) matter of the look-and-feel of my blog, or more specifically, my WordPress theme.</p>
<p>So who&#8217;s shoulders am I actually talking about.  Well firstly <a href="http://www.free-css-templates.com/">David Herreman</a> who is responsible for the clean and beautiful graphic design and equally clean css work that I used for my theme. I loved his <code>dkblog</code> css template and bookmarked it as soon as I saw it.  Secondly, <a href="http://www.blogohblog.com/">Bob </a>whose <code>big-blue-01</code> WordPress theme I used as a skeleton over which to stretch David&#8217;s stylesheet (I&#8217;m no PHP wizard!).  You&#8217;re looking at the result, unless I&#8217;ve changed theme since!  Thanks guys&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewwells.org.uk/blog/2007/12/standing-on-the-shoulders-of-giants/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

