top of page
Writer's pictureCristian Duque

Abstract Factory Pattern

Updated: Sep 1, 2023


The Abstract Factory pattern is a creational pattern from the GoF design patterns. It creates families of related objects through an interface without specifying the concrete implementations.


By using abstractions, the user can create one or more related factories. All the implementation details are hidden from the client's perspective.


A concrete factory creates a new instance of the associated objects. It's the responsibility of the factory to handle the instantiation process.


The following picture summarises the basic structure of the abstract factory pattern:

  • We have two interrelated objects, Product A and Product B.

  • A new factory creates the associated objects (Products) differently. E.g. A Bamboo factory paper and an Eucalyptus factory paper either implement Product A and Product B in a distinct way.

  • The user doesn't know anything about concrete implementations. Instead, it uses the factory interface, which outlines the features of the factory.


Keep in mind that design patterns offer solutions to common software problems. Specifically, the abstract factory design pattern is useful when dealing with multiple families of related products and ensuring that a family of related objects are used together.


Benefits

  • The user doesn't need to worry about implementation details. They can simply interact with the abstract factory interface to achieve their desired outcome by selecting a specific family of related objects.

  • Each concrete family implementation is independent, allowing for flexibility and extensibility in creating and representing individual factories without affecting other factories.

  • It adheres to the Open-Closed Principle (OCP) since new families can be added without disrupting existing code.

  • It also follows the Single-Responsibility Principle (SRP) by isolating the object creation responsibility for individual factories.

  • The Dependency Inversion Principle (DIP) is applied because the user depends on abstractions rather than implementation details.


Drawbacks

  • Higher code complexity by interacting with several interfaces and classes.

  • Factory constraints prevent using different types of objects that are not fully related, which can cause a mismatch in the abstract factory system.


Check out my video:


2 views

Recent Posts

See All

Comments


bottom of page