Abstract Factory | Cheat Sheet

Creational Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
3 min readJun 9, 2021

--

Abstract Factory UML

The Abstract Factory is is the most powerful tool among Creational Patterns. The variation of the creation of the products in the individual factories can be determined by Factory Method, Prototype, and Builder. The factory itself could be a Singleton.

An Abstract Factory provides an interface for creating a related family of objects, so the concrete classes are hidden and isolated behind it — it acts like a Facade. Objects belonging to an object family are instantiated together via a factory.

With that, it is very easy to add new classes to product families and extend the existing factories — complying to the Open-Closed-Principle.

Real-life examples

  • Things to be created together
  • Objects that form a theme (Font, Design, Color)
  • Product / Design sets (wall unit set from different parts)
  • Laptop construction with different specifications

Meaning

  • Interface for creating related object families
  • Hiding and isolating of concrete classes

Applicability

  • System should work independently of generation, composition, and representation of its objects
  • System is configured by multiple product families
  • Families are to be used together (constraint)
  • Only interfaces should be allowed to be accessed

Assets and Drawbacks

  • Isolation of concrete classes
  • Easy exchange of product families
  • Product consistency (compliance with the cooperation of products)
  • Support of new product types (preservation of the Open-Closed-Principle)

Extensibility

Example

First of all, you need some products that should be created together. For this example I chose to create a theme with different font and style classes.

Font classes
Style classes

After that, you can define the factories for the concrete classes above.

Factories for different themes

Next, you can define a client that holds the connection to the concrete factories. This can be a Facade or Singleton, too.

Client

At last, you can create different themes.

Creation of different themes

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and are a significant introduction into object composition (besides inheritance) and dependency inversion.

Creational Patterns are indispensable when abstracting the instantiation process of objects. Of high importance are encapsulation of concrete classes and hiding the creational process.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links