Abstract Factory Design Pattern

Abstract Factory ( Creational Design Pattern )



The abstract factory pattern is a software creational design pattern that provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.  or in similar way we can say as 
The Abstract Factory design pattern (part of the Gang of Four) falls under the Creational design pattern category and it provides a way to encapsulate a group of factories that have a common link without highlighting their concrete classes. 

The essence of the Abstract Factory Pattern is to "Provide an interface for creating families of related or dependent objects without specifying their concrete classes.





Abstract Factory patterns works around a super-factory which creates other factories. This factory is also called as Factory of factories. In Abstract Factory pattern an interface is responsible for creating a factory of related objects, without explicitly specifying their classes. This pattern is one level of abstraction higher than factory pattern.
 

As there is a word ‘abstract’ in the pattern name don’t mistake and confuse it with java ‘abstract’ keyword .  It considered as another layer of abstraction over factory pattern.
 

e.g. In Java while doing XML work with DOM Parser , we have used DocumentBuilderFactory   class which is an example abstract factory design pattern because it returns a factory called DocumentBuilder which then used to create Document.

//Example of Abstract Factory and Factory design pattern  in Java
DocumentBuilderFactory abstractFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder factory = abstractFactory.newDocumentBuilder();
Document doc = factory.parse(stocks)

In this example DocumentBuilderFactory (Abstract Factory) creates DocumentBuilder (Factory) which creates Documents (Products).

 In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the concrete objects that are part of the theme. The client does not know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface




Example : The best example will be configuring Database connection factory which hide underline Database type. We can configure Oracle, DB2 or Sybase Connection Factory in application.
    > java.util.Calendar#getInstance()
    > java.text.NumberFormat#getInstance()






progress

No comments: