Home Contact Sitemap

Matthew Wells

Java, J2EE & Configurable Systems

One Minute Patterns GOF State

March 23rd, 2011 by matthew

Here’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’s a simple example. (We’re all familiar with traffic lights!)
State Pattern Diagram
That’s it! What a state!
Got more than one minute? Read the rest of this entry »

Posted in Architecture, GOF, One Minute, Patterns, Patterns | Comments Off on One Minute Patterns GOF State

One Minute Patterns GOF Decorator

July 7th, 2008 by matthew

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’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’s a very simple example.
GOF Decorator Pattern (Simple)
That’s it! A Decorator. You may have used it already without realising!
Got more than one minute? Read the rest of this entry »

One Minute Patterns Introduction

July 4th, 2008 by matthew

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’d owned the seminal GOF Design Patterns book for 8 years and the Core J2EE Patterns book for five years, but I was still only really familiar with the most commonly referenced patterns.

What interested me even more was that once I started to study them, many of them represent very simple concepts that I’d been using in my designs for years.  Having studied the approach of the Head First Design Patterns 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’d love to hear from you!

Conditional Compilation in Java – Almost

February 28th, 2008 by matthew

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 #ifdef 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.

I’ve dabbled with various ways of achieving a similar effect in Java and so far this is the simplest approach that I’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.
Firstly, define all of the different flavours of product that you wish to build in a class ProductType.java. For example:

  1. public class ProductType {
  2. public static final String BANANAS = “bananas”;
  3. public static final String APPLES = “apples”;
  4. public static boolean isBananas() {
  5. return SetProduct.THIS_PRODUCT.equals(BANANAS);
  6. }
  7. public static boolean isApples() {
  8. return SetProduct.THIS_PRODUCT.equals(APPLES);
  9. }
  10. }

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 SetProduct.java class, which takes the following form:

  1. public final class SetProduct {
  2. public static final String THIS_PRODUCT = ProductType.BANANAS;
  3. }

The ant task for each product flavour copies the SetProduct.java class from the specified product directory into the correct location in your java source code tree.

  1. <target name=”bananas”>
  2. <copy overwrite=”true” todir=”src/mypackage”>
  3. <fileset dir=”products/bananas”>
  4. <include name=”SetProduct.java”>
  5. </fileset>
  6. </copy>
  7. </target>

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 SetProduct.java file is the only one that is required to support conditional compilation.)

Within your code base you can now define conditional blocks in the following way:

  1. if (ProductType.isBananas()){
  2. yellow = true;
  3. peelable = true;
  4. }
  5. else if (ProductType.isApples()){
  6. yellow = false;
  7. peelable = true;
  8. }

Desert Island Downloads

December 17th, 2007 by matthew

OK, so you’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’ll be a bit more specific. As Java’s my thing, I’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!)

Right at the top of my list, I would have to put xstream. In their own words:

XStream is a simple library to serialize objects to XML and back again.

I’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.

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’m still a big fan and the library has become an essential part of my toolkit for implementing configurable systems.

Standing on the Shoulders of Giants

December 15th, 2007 by matthew

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 & Jacobson; the Gang-of-Four, Richard Stallman, Linus Torvalds, Kent Beck and Martin Fowler are all names that spring easily to my mind.

However the individuals that I’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’m actually talking about the rather pedestrian (to some!) matter of the look-and-feel of my blog, or more specifically, my WordPress theme.

So who’s shoulders am I actually talking about. Well firstly David Herreman who is responsible for the clean and beautiful graphic design and equally clean css work that I used for my theme. I loved his dkblog css template and bookmarked it as soon as I saw it. Secondly, Bob whose big-blue-01 WordPress theme I used as a skeleton over which to stretch David’s stylesheet (I’m no PHP wizard!). You’re looking at the result, unless I’ve changed theme since! Thanks guys…