Introduction to Abstract Factory Design Pattern

Abstract factory design pattern provides an interface for creating families of related or dependent objects without a specifying their concrete classes.

The abstract factory design patter is very similar to factory method pattern. 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 subclass to handle the desired object instantiation.

Actually, the delegated objects frequently factory methods to perform the instantiation.

abstract factory design pattern

Applicability

Use the abstract factory pattern in any of following situation.

  • A system should be independent of how it’s products are created, composed, and represented
  • A class can’t anticipate the class of objects it must create
  • A system must use just one of a set of families of products

A family of related product objects is designed to be used together, and you need to enforce this constraint.

 

Participants

  • AbstractFactory

o Declare a interface for operations that create abstract products objects

  • Concretefactory

o Implements the operations to create concrete product objects

  • AbstractProduct

o Declare an interface for type of product object

  • ConcreteProduct

o Define a product object to be created by the corresponding concrete factory

o Implements the AbstractProduct interface

  • Client

o Uses only interfaces declare by AbstractFactory and AbstractProduct classes

 

Collaborations

Normally single instance of ConcreteFactory class is created at run time. (This is example of Singleton Pattern) This concrete factory creates product objects having particular implementations. To create different product objects, clients should use a different concrete factory.

AbstractFactory defers creation of product objects to its ConcreteFactory

 

Example

Let see how Abstractfactory design pattern applied to charting application which generate the chart depending upon user selection like bar chart, pie chart

  • First create an interface IChart which have drawChart method take a string arg

publicinterface IChart {

public void drawChart();

}

  • Create a abstract class say ChartAbstractFactory which have abstract method as getchart which takes string arg and return the interface type

publicabstractclass ChartAbstractFactory

publicabstract IChart getChart(String ChartType);

}

 

  • Create a class say BarChart and impelmets IChart interface

publicclass BarChart implements IChart {

@Override

public void drawChart () {

System.out.println(“charting Logic”);

}

}

 

  • Create a class ChartFactory which extends the ChartAbstractFactory

publicclass ChartFactory extends ChartAbstractFactory {

@Override

public void getChart(String chartType) {

if(chartType == null){

return null;

}

if(chartType == BarChart){

return new BarChart();

}

elseif(chartType ==PieChart)

{

return new PieChart();

}

}

return null;

}

  • Crate a class say FactoryProducer to get factories by passing information such as Chart

publicclassFactoryProducer {

    public static ChartAbstractFactory getFactory(String choice)

{

If(choice==chart)

{

return new ChartFactory();

}

return null;

}

}

  • Create a Client with main method

publicclass AbstractFactoryDemo{

ChartAbstractFactory factory = FactoryProducer.getFactory(“chart”);

IChart chart = factory. getChart(BarChart);

chart.drawChart();

}

 

Muqtar Ahmed

Helical IT Solutions