The Abstract Factory Method provides an interface to create and return one of several families of related objects. It provide an interface to create a family of related or dependent classes without specifying their concrete classes
Suppose you are writing a program to plan the layouts of the gardens. However no matter which kind of garden you are planning. Lets assumes garden can contains three types of plants
a. Border plants which would be planted on border side
b. Shaded Plants which would be planted for shading purpose
c. center Plants which would be planted on the center of garden
Lets code it. We have a base Garden class that contains main specifications of a garden of any type. So make it abstract.
public abstract class Garden {
public abstract Plant getBorder();
public abstract Plant getCenter();
public abstract Plant getShade();
}
Plant is a class that contains information about a plant of any type.
public class Plant {
private String name;
public Plant(String name) {
this.name = name;
}
public String getPlantName(){
return name;
}
}
Now in a real system, each type of garden would contains the plant information (infact a garden is a collection of many types of plant). Garden can of any type. Lets assumes there are three types of garden
first one is VegiGarden. Obviously this type of garden contains the features if Garden so :
public class VegiGarden extends Garden {
@Override
public Plant getBorder() {
return new Plant("Peas");
}
@Override
public Plant getCenter() {
return new Plant("Corn");
}
@Override
public Plant getShade() {
return new Plant("Neem");
}
}
next one is
public class AnnualGarden extends Garden {
@Override
public Plant getBorder() {
return new Plant("Bamboo");
}
@Override
public Plant getCenter() {
return new Plant("Mango");
}
@Override
public Plant getShade() {
return new Plant("Banyan");
}
}
and the last one is
public class FlowerGarden extends Garden {
@Override
public Plant getBorder() {
return new Plant("Rose");
}
@Override
public Plant getCenter() {
return new Plant("Carandola");
}
@Override
public Plant getShade() {
return new Plant("Popyi");
}
}
Now we have a GardenFactory that returns the object of requested garden type.
public class GardenFactory {
private String gardenType;
private String gardenName;
public GardenFactory(String gardenType) {
this.gardenType = gardenType;
if(gardenType.toUpperCase().equals("VG"))
gardenName="Vegitable Garden";
else if(gardenType.toUpperCase().equals("AG"))
gardenName="Annual garden";
else if(gardenType.toUpperCase().equals("FG"))
gardenName="Flower Garden";
else
gardenName="Illegal Garden";
}
public String getGardenName(){
return gardenName;
}
public Garden getGarden(){
if(gardenType.toUpperCase().equals("VG"))
return new VegiGarden();
else if(gardenType.toUpperCase().equals("AG"))
return new AnnualGarden();
else if(gardenType.toUpperCase().equals("FG"))
return new FlowerGarden();
else
return null;
}
}
We have last implementation class that starts making the gardens.
public class GardenMakerFactory {
public static void main(String[] args) {
GardenFactory flowerfactory=new GardenFactory("FG");
Garden fgarden= flowerfactory.getGarden();
System.out.println("Garden name is "+flowerfactory.getGardenName());
System.out.println("Specifications : ");
System.out.println("Border Plants : "+fgarden.getBorder().getPlantName());
System.out.println("Center Plants : "+fgarden.getCenter().getPlantName());
System.out.println("Shades Plants : "+fgarden.getShade().getPlantName());
GardenFactory vegitablefactory=new GardenFactory("VG");
Garden vgarden= vegitablefactory.getGarden();
System.out.println("Garden name is "+vegitablefactory.getGardenName());
System.out.println("Specifications : ");
System.out.println("Border Plants : "+vgarden.getBorder().getPlantName());
System.out.println("Center Plants : "+vgarden.getCenter().getPlantName());
System.out.println("Shades Plants : "+vgarden.getShade().getPlantName());
GardenFactory annualfactory=new GardenFactory("AG");
Garden agarden= annualfactory.getGarden();
System.out.println("Garden name is "+annualfactory.getGardenName());
System.out.println("Specifications : ");
System.out.println("Border Plants : "+agarden.getBorder().getPlantName());
System.out.println("Center Plants : "+agarden.getCenter().getPlantName());
System.out.println("Shades Plants : "+agarden.getShade().getPlantName());
}
}
Here FG cope is for Flower Garden , VG for Vegetable Garden and AG for Annual Garden
When we provides one of these codes, GardenFactory returns specific garden Object and the we can perform action of specified garden. Abstract factory pattern is typically used for giving implementation to specification (eg. jdbc servlet specifications etc...).
Tuesday, May 12, 2009
Abstract Factory [Creational Pattern]
Subscribe to:
Post Comments (Atom)
2 comments:
nice post.....
But i have one doubt & the doubt is that ,What is the difference between Abstract Factory Pattern and factory Pattern? When would you use each of them?
One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and
relies on a subclass to handle the desired object instantiation.
Post a Comment