RSS

Advanced Factory method Design pattern

28 Aug

Factory Design Pattern tutorial with design diagrams and code examples.

Overview

  • Scenario.

  • Description of normal factory method.

  • Description of extended factory method.

  • Design diagram of extended factory method.

Scenario

Think of a situation where you are asked to create a TOYOTA car. So you will happily finish your work by using “new” keyword as follows.

public abstract class Car {

public Car() {
}
public abstract String description();
}

Then there would be another request to create a BENZ car. So you would repeat the same procedure as follows.

But the problem arises when we need to create a HONDA car. Then we may use the same method as follows.

But as we can see we have to repeat the same procedure when a new car has to be created and this cause to change our core code frequently as requirements arise. So a better approach would be to provide a separate method to create the objects since these object creations might repeat many times in our code.

public class CarCreation {

Car myCar;
public Car createCar(String type){

if(type.equals("toyota")){
myCar = new ToyotaCar();
}else if (type.equals("benz")) {
myCar = new BenzCar();
}else {
myCar = new HondaCar();
}
return myCar;
}
}

Description of normal factory method

Here we have factored our code to a method and by this it will close the core code for modifications while keeping only this method to be exposed for changes. However, the best method will be to create a factory object out of this to keep this changeable part out of the core code. Therefore the factory method would be as follows.

public class CarFactory {

protected String type;
private Car myCar;

public CarFactory(String t) {
type = t;
}

public Car createCar(){

if(type.equals("toyota")){
myCar = new ToyotaCar();
}else if (type.equals("benz")) {
myCar = new BenzCar();
}else {
myCar = new HondaCar();
}
return myCar;
}
}

And the factory will be addressed at the run time as follows to retrieve the appropriate object via passing out parameters to factory.

public static void main(String[] args) {
Car car;
CarFactory carFactory = new CarFactory("toyota");
car = carFactory.createCar();
car.description();
}

As you may have already noticed all the cars (TOYOTA, BENZ and HONDA) are of “Car” type. Therefore we can provide an abstract Car class to impose constrains to keep all car types under same constrains. Abstract Car class can be initialized as follows.

public abstract class Car {

public Car() {
}
public abstract String description();
}

Therefore the concrete Car classes will be forced to follow a common structure as follows.

public class ToyotaCar extends Car{

@Override
public String description() {
return "TOYOTA";
}
}

The described Factory method can be displayed as follows;

Description of extended factory method

Ok, Now we are done with the factory method and we are having a factory and abstract Car class which will be sub classed to produce various cars from the factory as requested by the user at run time.

But wait What if there is a different user who needs a specialized car like a bullet proof vehicle. It’s obvious we can’t use the same factory to produce different cars so we need to go for a different factory. But anybody can come up with different kinds of factories having different skeletons.

And Java comes with various factory objects. Following is an example.

try {
XMLReader myReader = XMLReaderFactory.createXMLReader();
} catch (SAXException e) {
System.err.println(e.getMessage());
}

Is this the way we should impose a programmer to continue? Definitely not, that’s why we should take the design for a higher level. Because main goals of design patterns are to allow the code for extendibility and closed for modifications. Therefore we may provide an abstract Factory Class while providing the programmer to build factories as their wish while imposing a skeleton to work with. And the abstract Class will look as follows.

public abstract class AbstractCarFactory {

public AbstractCarFactory() {

}
protected abstract Car createCar(String type);

}

So this extended factory design method will allow programmers extra layer to work with allowing the code for extensions.

public class extendedCarFactory extends AbstractCarFactory{

protected String type;
private Car myCar;

public extendedCarFactory() {}

@Override
protected Car createCar(String type) {
if(type.equals("toyota")){
myCar = new ToyotaCar();
}else if (type.equals("benz")) {
myCar = new BenzCar();
}else {
myCar = new HondaCar();
}
return myCar;
}
}

We will craft the architecture by providing the abstract classes indicated in red color with special methods as described earlier. And the programmer will be provided the capability to acquire different car objects using the factory they created by sub classing the provided abstract factory class.

Design diagram of extended factory method

About these ads
 
2 Comments

Posted by on August 28, 2012 in Design patterns, Java

 

Tags: , ,

2 responses to “Advanced Factory method Design pattern

  1. Chathu

    August 28, 2012 at 11:24 am

    Nicely explained :) kep it up !

     

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 )

Google+ photo

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

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.