jobs4timesLogo jobs4timesLogo

Abstract Factory Design Pattern

  • Abstract Factory can be treated as a super factory or a factory of factories.
    Using factory design pattern we abstract the creation process of another class.
    Using the Abstract factory pattern we abstract the creation of family of classes.

  • Let's understand it by taking an example. We have several Dao's classes to persist the data. Like EmpDao, DeptDao etc., in-turn these Dao's can persist the data into a Database or into an XML file.
    So we have now the Dao's as DBEmpDao, DBDeptDao and XMLEMPDao, XMLDeptDao.

  • To create the objects of Dao's of related types we have create 2 different factories.
    DBDaoFactory and XMLDaoFactory, these factories takes care of creating the Dao's of their type.
package design;

public abstract class Dao{
 
 public abstract void save();

}
package design;

public class XmlEmpDao extends Dao{

@Override
public void save() {
 System.out.println("Employee has been written to XML file");
}
	
}
package design;

public class DBEmpDao extends Dao{

@Override
public void save() {
 System.out.println("Employee has been written to table");
}
	
}
package design;

public class XMLDeptDao extends Dao{

@Override
public void save() {
 System.out.println("Department has been written to XML File");		
}

}
package design;

public class DBDeptDao extends Dao{

@Override
public void save() {
 System.out.println("Department has been written to table");		
}

}
Now we have XmlDaoFactory and DBDaoFactory which creates Dao objects of their type.
package design;

public abstract class DaoFactory {
	public abstract Dao createDao(String string) ;
}
package design;

public class XMLDaoFactory extends DaoFactory{

public Dao createDao(String type){
 Dao dao=null;
 if(type.equals("emp")){
  dao=new XmlEmpDao();
 }else if(type.equals("dept")){
	 dao=new XMLDeptDao();
 }
 return dao;
}

}
package design;

public class DBDaoFactory extends DaoFactory{

public Dao createDao(String type){
 Dao dao=null;
 if(type.equals("emp")){
  dao=new DBEmpDao();
 }else if(type.equals("dept")){
	 dao=new DBDeptDao();
 }
 return dao;
}

}


package design;

public class DaoMaker {

public static DaoFactory make(String factoryType){
 DaoFactory df=null;
 if(factoryType.equals("xml")){
   df=new XMLDaoFactory();
 }else if(factoryType.equals("db")){
   df=new DBDaoFactory();
 }
 return df;
}

}
  • now the DaoMaker will takes care of instantiating the appropriate factory to work with family of Dao's.
  • For any application it is important to use all Dao's that belongs to same type.
  • So our DaoMaker enforces this rule by encouraging you to get one type of factory from which you can Dao's.
For example if an application wants a Dao object it has to do the following.
package design;

public class AbstractFactoryTest {

public static void main(String[] args){
 DaoFactory daoFactory=null;
 Dao dao=null;

 daoFactory=DaoMaker.make("xml");
 dao=daoFactory.createDao("emp");
 dao.save();
}

}
  • In the above example the client is using "xml" family of Dao's to perform operations.
  • If we want to switch from "xml" to "db" he don't need to make lot of modifications as he is dealing with DaoFactory abstract class, he can easily switch between any of the implementation of DaoFactory by calling DaoMaker.make("db").

  • Hence Abstract Factory pattern helps you in enforcing your application to use related classes across the application.
The UML Diagram for the AbstractFactory :
abstract-factory-design-pattern
Difference between AbstractFactory and FactoryMethod :
  • Abstract Factory pattern delegates the responsibility of object instantiation to another object via composition.
  • Factory Method pattern uses inheritance and relies on subclasses to handle the desired object instantiation.
BACK