Skip to content

Abstract Factory

Roberto Fronteddu edited this page Jan 31, 2023 · 3 revisions

The Abstract Factory is a Creational Design Pattern. Separate families of objects meant to work together behind the concrete implementation of a factory interface.

Usefull when

  • We have two or more objects that work together to form a kit or set and there can be multiple sets or kits can be created by client code.
  • You want to constrain object creations so that they all work together ("they form a family of objects", for example, think about creating car components and bike components, they are all designed to work together either to build a car or to build a bike).

Example

builder-uml

  • AbstractProductA is an interface, implemented by ProductAOne and ProductATwo. Similarly, for AbstractProductB there are two child classes.
  • ProductAOne and ProductBOne form a set that is meant to work together.
  • ProductATwo and ProductBTwo form a set that is meant to work together.
  • Abstract Factory keeps the logic of the two sets clean and separated by having a separate implementation of AbstractFactory for each set.

Implementation Steps

  1. Study the product sets
    • Create an abstract factory as an abstract class/interface defining the methods for creating products.
    • Provide a concrete implementation for each set
  2. We can make use of the factory method pattern to produce an object with multiple factory methods.

Considerations

  • Factories can be implemented as singletons, we typically ever need only one instance of it anyway.
  • Adding a new product type requires changes to the base factory as well as all implementations of the factory.
  • The client code is provided with the factory
  • Uses factory method pattern
  • If objects are expensive to create, it is possible to transparently switch factory implementations to use prototype design patterns to create objects.

Pitfalls

  • More complex than the factory method.
  • Adding a new product requires changes to the base factory as well as all the implementations of the factory
  • Difficult to visualize the need at the start of development and usually starts out as a factory method.

Comparison with factory method:

Factory Method:

  • Hides the concrete objects which are used from the client code.
  • Is concerned with one product and its subclasses.
  • Collaboration of the product itself with other objects is irrelevant.

Abstract Factory

  • Hides factories as well as concrete objects used from the client code.
  • Suitable when multiple objects are designed to work together and the client must use products from a single family at a time.
  • Very specific to the problem of a "product of families" that work together.

Real-Life Example:

  • java.xml.parsers.DocumentBuilderFactory (excluding the static newInstance method which returns the actual factory class object, note that this method uses classpath scanning instead of having hardcode in it so we can change the factory used in it)
Clone this wiki locally