Monday, February 28, 2011

Factory pattern

Factory of what? Of classes. In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.
Let’s take an example to understand this pattern.
The Base class

public class Person {
// name string
public String name;
// gender : M or F
private String gender;

public String getName() {
return name;

public String getGender() {
return gender;
}// End of class
This is a simple class Person having methods for name and gender. Now, we will have two sub-classes, Male and Female which will print the welcome message on the screen.

public class Male extends Person {
public Male(String fullName) {
System.out.println("Hello Mr. "+fullName);
}// End of class
Also, the class Female

public class Female extends Person {
public Female(String fullNname) {
System.out.println("Hello Ms. "+fullNname);
}// End of class
Now, we have to create a client, or a SalutationFactory which will return the welcome message depending on the data provided.

public class SalutationFactory {
public Person getPerson(String name, String gender) {
if (gender.equals("M"))
return new Male(name);
else if(gender.equals("F"))
return new Female(name);
else return null;
}// End of class

This class accepts two arguments from the system at runtime and prints the names.

Running the program:

public class FactoryPatternDemo{
public static void main(String args[]) {
SalutationFactory factory = new SalutationFactory();
factory.getPerson("Kinshuk", "M");

The result returned is:

“Hello Mr. Kinshuk”.

When to use a Factory Pattern?

The Factory patterns can be used in following cases:
1. When a class does not know which class of objects it must create.
2. A class specifies its sub-classes to specify which objects to create.
3. In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.

Download source

You can download the source code of this program from here.

No comments:

Post a Comment