Tuesday, May 12, 2009

Factory Pattern [Creational Patterns]

Creational Patterns

All the creational patterns define the best possible way in which an object can be instantiated. These describes the best way to CREATE object instances. Now everyone knows the object instance in Java can be created using a new operator.

Book book = new Book ();

So, what’s the great stuff? Well, that’s true. The new Operator creates the instance of an object, but this is hard-coding. As I have said earlier, creating good software is difficult and so, hard coding is the last thing one should do. Also, at times the very nature of the object which is created can change according to the nature of the program. In such scenarios, we can make use of patterns to give this a more general and flexible approach.

There are five types of Creational Patterns.
1. Factory Pattern

2. Abstract Factory Pattern

3. Builder Pattern

4. Prototype Pattern

5. Singleton Pattern


I'll be explaining each in detail.


So lets start with Factory Pattern

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.

Example: Let’s suppose an application asks for entering the name and sex of a person. If the sex is Male (M), it displays welcome message saying Hello Mr. and if the sex is Female (F), it displays message saying Hello Ms .

The skeleton of the code can be given here.

public class Person {

private String name;
private String gender;

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}


where as Male and female classes should be like the below

public class Male extends Person{

public Male(String name) {
setName("Mr. "+name);
setGender("Male");
}
}


public class Female extends Person {

public Female(String name) {
setGender("Female");
setName("Ms. "+name);
}
}


and its factory class should be like below :

public class SalutationFactory {

public static void main(String args[]) {
SalutationFactory factory = new SalutationFactory();
//System.out.println("Hello "+factory.getPerson("gaurav", "M").getName());
// OR
Person person=factory.getPerson("Gaurav", "M");
System.out.println("Hello "+person.getName());
}

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;
}
}
}


When to use a Factory Pattern?
The Factory patterns can be used in following cases:
1. 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.

2 comments:

Unknown said...

very nice post. plz keep it up further

Unknown said...

Great post........