Abstract Factory Design Pattern in Java

Introduction

Abstract Factory pattern is one of the most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. Abstract Factory patterns works around a parent-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. Each generated factory can give the objects as per the Factory pattern.

Implementation

We will create Building and PaintCompany interfaces and concrete classes implementing these interfaces. We will also create an abstract factory class AbstractFactory later. Factory classes BuildingFactory and PaintFactory are defined where each factory extends AbstractFactory. A factory creator or generator class FactoryProducer is created. The demo class AbstractFactoryPatternDemo uses FactoryProducer to get a AbstractFactory object. It will pass information (Home / House / Hut for Building) to AbstractFactory to get the type of object it needs. It also passes information (Asian / Berger / Nerolac for Paint Company) to AbstractFactory to get the type of object it needs.

Class Diagram

abstract factory pattern in Java

The following steps illustrate how to create abstract factory pattern

Step 1. Create a Building interface. This interface has only one method build() which will be implemented by different classes to build different kind of buildings.

Step 2. Create concrete classes to implement the Building interface.

The below class builds a Home.

The below class builds a House.

The below class builds a Hut.

Step 3. Create a PaintComapny interface. This interface has only one method paint() which will be implemented by different classes to paint from the different paint company.

Step 4. Create concrete classes to implement the PaintComapny interface.

The below class paints from Asian paint company.

The below class paints from Berger paint company.

The below class paints from Nerolac paint company.

Step 5. Create an Abstract class to get factories for Building and PaintComapny Objects.

Step 6. Create Factory classes extending AbstractFactory to generate object of concrete class based on given information.

The below class is for BuildingFactory.

The below class is for PaintFactory.

Step 7. Create a Factory generator or producer class to get factories by passing an information such as Building type or Paint company

Step 8. Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing information such as type.

Step 9. Run the class AbstractFactoryPatternDemo. Verify the output

That’s all. Thank you for your reading.

 

 

 

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.