Abstract Factory Design Pattern

In this post, we will learn how to implement the Abstract Factory Design Pattern in Java with step by step example.

Abstract Factory Design Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

In plain words, A factory of factories; a factory that groups the individual but related/dependent factories together without specifying their concrete classes.

Abstract Factory Design Pattern Structure


1. AbstractFactory 
  • declares an interface for operations that create abstract product objects.
2. ConcreteFactory 
  • implements the operations to create concrete product objects.
3. AbstractProduct
  • declares an interface for a type of product object.
4. ConcreteProduct
  • defines a product object to be created by the corresponding concrete factory.
  • implements the AbstractProduct interface.
5. Client
  • uses only interfaces declared by AbstractFactory and AbstractProduct classes.

Abstract Factory Design Pattern Implementation

To create a kingdom we need objects with a common theme. Elven kingdom needs an Elven king, Elven castle, and Elven army whereas Orcish kingdom needs an Orcish king, Orcish castle, and Orcish army. There is a dependency between the objects in the kingdom.

Translating the kingdom example above. First of all, we have some interfaces and implementation for the objects in the kingdom.

public interface Castle {
  String getDescription();
}

public interface King {
  String getDescription();
}

public interface Army {
  String getDescription();
}

// Elven implementations ->
public class ElfCastle implements Castle {
  static final String DESCRIPTION = "This is the Elven castle!";
  @Override
  public String getDescription() {
    return DESCRIPTION;
  }
}
public class ElfKing implements King {
  static final String DESCRIPTION = "This is the Elven king!";
  @Override
  public String getDescription() {
    return DESCRIPTION;
  }
}
public class ElfArmy implements Army {
  static final String DESCRIPTION = "This is the Elven Army!";
  @Override
  public String getDescription() {
    return DESCRIPTION;
  }
}

// Orcish implementations similarly -> ...

Then we have the abstraction and implementations for the kingdom factory

public interface KingdomFactory {
  Castle createCastle();
  King createKing();
  Army createArmy();
}

public class ElfKingdomFactory implements KingdomFactory {
  public Castle createCastle() {
    return new ElfCastle();
  }
  public King createKing() {
    return new ElfKing();
  }
  public Army createArmy() {
    return new ElfArmy();
  }
}

public class OrcKingdomFactory implements KingdomFactory {
  public Castle createCastle() {
    return new OrcCastle();
  }
  public King createKing() {
    return new OrcKing();
  }
  public Army createArmy() {
    return new OrcArmy();
  }
}

Now we have the abstract factory that lets us make a family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc.

var factory = new ElfKingdomFactory();
var castle = factory.createCastle();
var king = factory.createKing();
var army = factory.createArmy();

castle.getDescription();
king.getDescription();
army.getDescription();

Program output:

This is the Elven castle!
This is the Elven king!
This is the Elven Army!

Now, we can design a factory for our different kingdom factories.

Applicability

Use the Abstract Factory pattern when

  • The system should be independent of how its products are created, composed, and represented
  • The system should be configured with one of multiple families of products
  • The family of related product objects is designed to be used together, and you need to enforce this constraint
  • You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
  • The lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
  • You need a run-time value to construct a particular dependency
  • You want to decide which product to call from a family at runtime.
  • You need to supply one or more parameters only known at run-time before you can resolve a dependency.
  • When you need consistency among products
  • You don’t want to change the existing code when adding new products or families of products to the program.

Known uses