January 16, 2021
Hot Topics:

Working With Design Patterns: Abstract Factory

  • By Jeff Langr
  • Send Email »
  • More Articles »

Like many design patterns, the abstract factory pattern and the builder pattern appear very similar in terms of their class structure. They are both creational patterns used to construct complex object families. However, the builder pattern has an emphasis on step-by-step construction of each part of the composite structure, whereas the abstract factory is designed to return a constructed object immediately.

Client code plays different roles in the two pattern implementations. Generally, when using the builder pattern, the client knows the component types to be passed into the builder. With an abstract factory, the client requests an implementation of an interface, and has no idea about the specific implementation types of the returned object structure.

To best contrast the differences between abstract factory and builder, I'll use the same example that I presented in my article on the builder pattern: In a library, a catalog is a collection of various materials—books and movies (see Listing 1). Libraries print various reports on a frequent basis, including a rather large inventory report that lists all materials in the library, sorted first by material type. These reports can go to either the web or to a simple text-based desktop user interface. (Reuse is such a great thing that I've even been able to reuse a bit of text here!)

Listing 1: Basic materials.

// Material.java:
public class Material {
   private String classification;
   private String title;
   private String director;
   private String year;

   public Material(String classification, String title,
                   String director, String year) {
      this.classification = classification;
      this.title = title;
      this.director = director;
      this.year = year;

   public String getAuthor() {
      return director;

   public String getClassification() {
      return classification;

   public String getTitle() {
      return title;

   public String getYear() {
      return year;

// Book.java:
public class Book extends Material {
   public Book(String classification, String title,
               String author, String year) {
      super(classification, title, author, year);
   // ...

// Movie.java:
public class Movie extends Material {
   public Movie(String classification, String title,
                String director, String year) {
      super(classification, title, director, year);

   public String getDirector() {
      return getAuthor();
   // ...

The Catalog class is a simple container of materials. See Listing 2.

Listing 2: The Catalog class.

import java.util.*;

public class Catalog implements Iterable<Material> {
   private List<Material> materials = new ArrayList<Material>();

   public void add(Material material) {

   public List<Material> materials() {
      return materials;

   public Iterator<Material> iterator() {
      return materials.iterator();

The goal of a reporting subsystem is to take a client interest in platform (web or desktop), plus the list of materials, and produce a report appropriate to the platform. The client specifies the platform using an enum (see Listing 3). The implementation this time around will use the abstract factory pattern. As the builder article states, the steps to construct a report are simple: Print a header, iterate through a sorted list of the materials and print a row for each, and finally print a summary section.

Listing 3: The Platform enum.

public enum Platform {

Page 1 of 4

This article was originally published on September 4, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date