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:

If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.

Go to your working directory and then follow below steps:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout abstract-factory

If you don’t have git installed, you can either directly download zip or copy paste from below.

Bike.java

Superbike.java

Scooter.java

Engine.java

SuperbikeEngine.java

ScooterEngine.java

TyreSet.java

SuperbikeTyreset.java

ScooterTyreSet.java

PartsFactory:

SuperbikePartsFactory:

ScooterPartsFactory:

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

4 comments for “Abstract Factory Pattern Implementation

  1. Nyng
    April 4, 2017 at 2:54 pm

    I think you forgot the code of most important parts: the PartsFactory: SuperbikePartsFactory, and PartsFactory: ScooterPartsFactory.

    But it’s ok, all concepts are clear to me.

    • April 4, 2017 at 5:06 pm

      Indeed it seems I have missed some things 🙂 I will update everything in 24 hours. Also I have created a git repo for all design patterns. You can directly clone them from there. I will share everything soon.

    • April 4, 2017 at 5:17 pm

      Please check now I have updated the article 🙂 Also, I will be sharing git url for each design pattern soon.

Leave a Reply

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