Abstract Factory Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In last article we learned about factory method design pattern. This article will help understand another flavor i.e. abstract factory pattern. When systems become more complex, abstract factory pattern implementation is useful.

Abstract Factory Pattern Implementation:

Abstract factory pattern implementation provides us a framework that allows us to create objects that follow a general pattern. So at runtime, abstract factory is coupled with any desired concrete factory which can create objects of desired type.

Definition:

Gang of Four book defines abstract factory pattern as below

The abstract factory pattern provides an interface for creating families of related or dependent objects without specifying concrete classes.

  • Abstract factory gives us an interface for creating family of products, where family means a group of similar type of objects.
  • Actual calling code is decoupled from factory that creates products.
  • Abstract factory pattern implementation defines interface that all concrete factories must implement which consist of set of methods for producing products.
  • Often methods of abstract factories are implemented as factory methods.
  • Generally abstract factory pattern implementation relies on composition rather than inheritance.

Class Diagram:

A Generic Abstract Factory Pattern

A Generic Abstract Factory Pattern

  • AbstractFactory  : Declares a way for operations that create abstract products.
  • ConcreteFactory : Implements operations to create concrete products.
  • Product              : Declares product to be created by corresponding ConcreteFactory
  • Client                 : Uses AbstractFactory and AbstractProduct classes.

Abstract Factory Pattern Implementation Example:

We will continue with our favorite Bike models. Consider we want to create a factory a bikes where each type of bike will have some common attributes but they will differ according to specifications.

  • Bike is an abstract class. It has fields Engine and TyreSet which are abstract products.
  • Bike has paint() method and abstract prepare() method which needs to be implemented by concrete subclasses.
  • Superbike and Scooter extends Bike class and implement prepare() method which prepares superbike and scooter according to their specifications.

BikeHierarcny

There is separate hierarchy for Engine and TyreSet also.

  • Engine is an abstract product, SuperbikeEngine and ScooterEngine implement it.
  • TyreSet is an abstract product, SuperbikeTyreSet and ScooterTyreSet implement it.

PartsHirarchy

 

Now we are going to define abstract factories for Engine and TyreSet

  • PartsFactory is abstract class which creates parts, i.e. Engine and TyreSet.
  • createEngine() method creates Engine while createTyreSet() method creates TyreSet
  • SuperbikePartsFactory extends PartsFactory and implement both methods to create superbike specific Engine and TyreSet.
  • ScooterPartsFactory extends PartsFactory and implement both methods to create scooter specific Engine and TyreSet

FactoryHierarchy

 

Although not needed, I’ve created hierarchy for workshops also

  • Workshop is abstract class which has public method assembleBike() and factory method createBike().
  • SuperbikeWorkshop and ScooterWorkshop extends Workshop and implement createBike() method specific to each type of bike.

Workshops Hierarchy

Abstract Factory Pattern Implementation:

Bike.java

Superbike.java

Scooter.java

Engine.java

SuperbikeEngine.java

ScooterEngine.java

TyreSet.java

SuperbikeTyreset.java

ScooterTyreSet.java

Workshop.java

SuperbikeWorkshop.java

ScooterWorkshop.java

Dealer.java

Run Dealer.java and it will output,

Let us study this abstract factory pattern implementation with class diagram of our example.

BikeExampleDesignPattern

This is how program flows.

  • Dealer class instantiates SuperbikeWorkshop and ScooterWorkshop which act as clients.
  • SuperbikeWorkshop implements createBike() method and instantiates SuperbikePartsFactory, ScooterWorkshop does same for ScooterPartsFactory
  • createBike() method in SuperbikeWorkshop instantiates a Superbike by passing SuperbikePartsFactory instance to Superbike constructor. i.e. Bike superbike = new Superbike(superbikePartsFactory); instance is returned to calling method. Same for Scooter also.
  • concrete createBike() is called from assembleBike() where we perform further operations.
  • prepare() method is called on bike instance i.e. a Superbike or Scooter instance.
  • prepare() method implementation of each class calls respective respective PartsFactory methods and set the parts to bike. i.e. a Superbike instance is created with SuperbikeEngine and SuperbikeTyreSet. Same for Scooters also.

This is rather long article, but I hope this article helps understand abstract factory pattern implementation.

Share Button

1 comment for “Abstract Factory Pattern Implementation

Leave a Reply

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