Abstract Factory Pattern
November 11th, 2009Factory of factories. To keep things simple you can understand it like, you have a set of ‘related’ factory method design pattern. Then you will put all those set of simple factories inside a factory pattern. So in turn you need not be aware of the final concrete class that will be instantiated. You can program for the interface using the top factory.
There is also a view that abstract factory is ‘also’ implemented using prototype instead of factory methords pattern. Beginners for now please don’t yourself with that. Just go with factory methods pattern.
As there is a word ‘abstract’ in the pattern name don’t mistake and confuse it with java ‘abstract’ keyword. It is not related to that. This abstract is from object oriented programming paradim.
Sample abstract factory design pattern implementation in Java API
XML API implements abstract factory. There is a class name SchemaFactory. This acts as a factory and supports implemenation of multiple schemas using abstract factory design pattern.
Sample Java Source Code for Factory Method Design Pattern
Following is the interface, that will be returned as the final end product from the factories.
package com.javapapers.sample.designpattern.abstractfactory;
public interface Animal {
public void breathe();
}
Following is the interface for which the factory implementation should be done. Inturn all abstract factory will return this type.
package com.javapapers.sample.designpattern.abstractfactory;
public interface AnimalFactory {
public Animal createAnimal();
}
One of the factory from a predefined set which will instantiate the above interface.
package com.javapapers.sample.designpattern.abstractfactory;
public class SeaFactory implements AnimalFactory {
public Animal createAnimal() {
return new Shark();
}
}
Second factory from a predefined set which will instantiate the Animal interface.
package com.javapapers.sample.designpattern.abstractfactory;
public class LandFactory implements AnimalFactory {
public Animal createAnimal() {
return new Elephant();
}
}
Implementation of an Animal. This class is grouped with the first abstract factory.
package com.javapapers.sample.designpattern.abstractfactory;
public class Shark implements Animal {
public void breathe() {
System.out.println("I breathe in water! He he!");
}
}
Implementation of an Animal. This class is grouped with the second abstract factory.
package com.javapapers.sample.designpattern.abstractfactory;
public class Elephant implements Animal {
public void breathe() {
System.out.println("I breathe with my lungs. Its easy!");
}
}
Following class consumes the abstract factory.
package com.javapapers.sample.designpattern.abstractfactory;
public class Wonderland {
public Wonderland(AnimalFactory factory) {
Animal animal = factory.createAnimal();
animal.breathe();
}
}
Testing the abstract factory design pattern.
package com.javapapers.sample.designpattern.abstractfactory;
public class SampleAbstractFactory {
public static void main(String args[]){
new Wonderland(createAnimalFactory("water"));
}
public static AnimalFactory createAnimalFactory(String type){
if("water".equals(type))
return new SeaFactory();
else
return new LandFactory();
}
}
Output of the above sample program for abstract factory pattern
I breathe in water! He he!



hey thanks for gr8 tutorials. Can you also add singleton pattern as it is widely asked question in interviews.
I am working on tutorials for all GOF design patterns and will post them one by one soon.
Will post singleton design pattern within couple of weeks.
By the way, your website is cool Priyan!
[...] Abstract factory may also be used to construct a complex object, then what is the difference with builder pattern? In builder pattern emphasis is on ’step by step’. Builder pattern will have many number of small steps. Those every steps will have small units of logic enclosed in it. There will also be a sequence involved. It will start from step 1 and will go on upto step n and the final step is returning the object. In these steps, every step will add some value in construction of the object. That is you can imagine that the object grows stage by stage. Builder will return the object in last step. But in abstract factory how complex the built object might be, it will not have step by step object construction. [...]
Builder Pattern&hellip on November 17th, 2009 9:21 pm