December 4, 2007

THE FACTORY PATTERN

One type of pattern that we see again and again in OO programs is the
Factory pattern or class. A Factory pattern is one that returns an instance of
one of several possible classes depending on the data provided to it. Usually
all of the classes it returns have a common parent class and common methods,
but each of them performs a task differently and is optimized for different
kinds of data.

How a Factory Works


Sample Code
Let's consider a simple case where we could use a Factory class. Suppose we
have an entry form and we want to allow the user to enter his name either as
“firstname lastname” or as “lastname, firstname”. We’ll make the further
simplifying assumption that we will always be able to decide the name order
by whether there is a comma between the last and first name.
3
This is a pretty simple sort of decision to make, and you could make it with a
simple if statement in a single class, but let’s use it here to illustrate how a
factory works and what it can produce. We’ll start by defining a simple base
class that takes a String and splits it (somehow) into two names:


class Namer {
//a simple class to take a string apart into two names
protected String last; //store last name here
protected String first; //store first name here
public String getFirst() {
return first; //return first name
}
public String getLast() {
return last; //return last name
}
}


In this base class we don’t actually do anything, but we do provide
implementations of the getFirst and getLast methods. We’ll store the split
first and last names in the Strings first and last, and, since the derived classes
will need access to these variables, we’ll make them protected.
The Two Derived Classes
Now we can write two very simple derived classes that split the name into
two parts in the constructor. In the FirstFirst class, we assume that everything
before the last space is part of the first name:


class FirstFirst extends Namer { //split first last
public FirstFirst(String s) {
int i = s.lastIndexOf(" "); //find sep space
if (i > 0) {
//left is first name
first = s.substring(0, i).trim();
//right is last name
last =s.substring(i+1).trim();
}
else {
first = “”; // put all in last name
last = s; // if no space
}
}
}


And, in the LastFirst class, we assume that a comma delimits the last name.
In both classes, we also provide error recovery in case the space or comma
does not exist.

class LastFirst extends Namer { //split last, first
public LastFirst(String s) {
int i = s.indexOf(","); //find comma
if (i > 0) {
//left is last name
last = s.substring(0, i).trim();
//right is first name
first = s.substring(i + 1).trim();
}
else {
last = s; // put all in last name
first = ""; // if no comma
}
}
}


Building the Factory
Now our Factory class is extremely simple. We just test for the existence of a
comma and then return an instance of one class or the other:


class NameFactory {
//returns an instance of LastFirst or FirstFirst
//depending on whether a comma is found
public Namer getNamer(String entry) {
int i = entry.indexOf(","); //comma determines name
order
if (i>0)
return new LastFirst(entry); //return one class
else
return new FirstFirst(entry); //or the other
}
}


Using the Factory


You type in a name and then click on the Compute button, and the divided
name appears in the text fields below. The crux of this program is the
compute method that fetches the text, obtains an instance of a Namer class
and displays the results.
In our constructor for the program, we initialize an instance of the factory
class with

NameFactory nfactory = new NameFactory();

Then, when we process the button action event, we call the computeName
method, which calls the getNamer factory method and then calls the first and
last name methods of the class instance it returns:

private void computeName() {
//send the text to the factory and get a class back
namer = nfactory.getNamer(entryField.getText());
//compute the first and last names
//using the returned class
txFirstName.setText(namer.getFirst());
txLastName.setText(namer.getLast());
}

And that’s the fundamental principle of Factory patterns. You create an
abstraction which decides which of several possible classes to return and
returns one. Then you call the methods of that class instance without ever
knowing which derived class you are actually using. This approach keeps the
issues of data dependence separated from the classes’ useful methods. You
will find the complete code for Namer.java

1 comment:

Susantha said...

nice man, I learn it. But i think this kind of mechanism we use MVC in Struts too. Model,View,Controller.
Model - code which include extended.

View - your input jsp or asp page.

Controller - Factory code which include if condition to control.

isn't it?