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

Comments

  1. Abstract factory design pattern has a totally different structure from a normal pattern. Maybe, it even works another way. Anyway, I must try.

    ReplyDelete
  2. What a perfect step-by-step guide. I am sure this post will be very helpful to beginners because everything is really understandable.

    ReplyDelete
  3. Looking for Name Patches for your businesses ? Then we are the best for you, who suit your expectations. We have huge experience on this field. We are 100% dedicated to find you cheap Name Patches.
    We make custom patches for your own need. You can buy cheap Name Patches safely here.
    If you want to know more, Please check out here : Velcro Name Patches

    ReplyDelete
  4. Implementation of source code in a way where people can understand the abstract factory design pattern is really hard, but after reading your post I've to admit that you have full knowledge about this. Cheap Dissertation Writing Services

    ReplyDelete
  5. Hay, This is Arusa. I like this website pretty much. Great info. Hi and thanks for the web blogs.
    https://www.professionalessayservice.co.uk/blog/5-tips-to-improve-your-academic-writing-skills

    ReplyDelete
  6. I love your content and the lessons you share with your readers. Every time I read a post I feel like Buy custom assignment online uk I am able to take advantage of this post.

    ReplyDelete
  7. Java is one of the leading languages and its user- friendliness and flexible features makes it a ‘go-to’ language for most of the web developers.

    Java Training Institute in Delhi

    ReplyDelete
  8. It's really a piece of new information to me, I learn more from this blog, I want to learn more from this blog, keep on it doing, I eagerly waiting for your updates, thank you!!

    java training institute in delhi

    ReplyDelete

Post a comment