Using the Factory Pattern

February 6, 2010 at 12:16 am Leave a comment

It has been a long time since I posted anything about Design Patterns but as we aware or even unaware our daily coding practices relate somehow or another to a pattern.  It might not necessarily be documented but it generally forms part of a pattern.

Background

I have recently begun working with ICEfaces and Fusion Charts in order to render various types of charts.

The model used is as follows:

  • Chart data element storing chart specific data
  • Chart generator that uses xstream to convert this chart data into XML.

We then created chart data and a generator per type of chart. Eg. Piechart data and piechart XML generator.

So now when we want to get a specific generator we need to actually do the check to see what type of chart we have then determine the generator.  The check could look something like this:

Chart chart // this could be any subclass, such as a PieChart
if (chart instance of PieChart) {
return PieChartXmlGenerator;
} else if (chart instance of LinearGaugeChart) {
return LinearGaugeChartXmlGenerator;
} .....
....
....

This can become cumbersome since each time we require a specific generator we need to do this check.

Analogy

When trying to think of the best analogy to describe this pattern I thought about the building contractor relationship. Lets say I want to tile my bathroom, I would inform my contractor about this and he would bring me a tiler. If I wanted to build some cupboards he would bring me a carpenter. Simple enough? In this analogy the contractor is our “factory”. We simply specify what we looking for and the “factory” returns what we need to get the task done.

Example

The code above where we did the “instanceof” check could all be moved into a Factory like this:

public class ChartXmlGeneratorFactory {

 public static ChartXmlGenerator getChartXmlGenerator(Chart chart) {
 if (chart instanceof PieChart) {
    return new PieChartXmlGenerator();
 } else if (chart instanceof LinearGaugeChart){
   return new LinearGaugeXmlGenerator();
 } // add new ChartGenerator selections here
   throw new IllegalStateException("The chart returned does not match any of the implemented Chart Types");
 }
}

Then when we want to actually use the factory we would just call it like this:

PieChart pieChart = new PieChart();
PieChartXmlGenerator chartXmlGenerator = (PieChartXmlGenerator) ChartXmlGeneratorFactory.getChartXmlGenerator(pieChart);

I omitted defining PieChartXmlGenerator :-)   but I hope you get my point. This is the nice way of simply saying to your factory I want to do this and your factory says use this to do what you require.

This is the Java version of our factory, I am getting to grips with Spring and will post about the Spring alternative to this. Its really very good.

Please feel free to post any comments or suggestions you may have. Again I would like to reiterate that I am no expert in this topic but simply documenting my journey of discovery.
Blog Soon!

Advertisement

Entry filed under: Design Patterns. Tags: .

Trying to Understand Maven EasyMock – JUnit Tests

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


 

February 2010
M T W T F S S
« Jan   May »
1234567
891011121314
15161718192021
22232425262728

Categories


Follow

Get every new post delivered to your Inbox.