Design Patterns Explained
  • Getting Started
  • SOLID Principles
    • Single Responsibility Principle
    • Open / Closed Principle
    • Liskov Substitution Principle
    • Interface Segregation Principle
    • Dependency Inversion Principle
  • Design Patterns
    • Creational Patterns
      • Abstract Factory Pattern
      • Builder Pattern
      • Factory Pattern
      • Prototype Pattern
      • Singleton Pattern
    • Behavioral Patterns
      • Chain Of Responsibility Pattern
      • Command Pattern
      • Interpreter Pattern
      • Iterator Pattern
      • Mediator Pattern
        • Example 1
      • Memento Pattern
      • Observer Pattern
      • State Pattern
      • Strategy Pattern
        • Example 1
      • Template Method Pattern
      • Visitor Pattern
    • Structural Patterns
      • Adapter Pattern
      • Bridge Pattern
      • Composite Pattern
      • Decorator Pattern
      • Facade Pattern
      • Flyweight Pattern
      • Proxy Pattern
  • Roadmap
Powered by GitBook
On this page
  • Intent
  • When to use
  • Structure
  • Note
  • Examples

Was this helpful?

  1. Design Patterns
  2. Creational Patterns

Abstract Factory Pattern

PreviousCreational PatternsNextBuilder Pattern

Last updated 4 years ago

Was this helpful?

Intent

Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.

When to use

  1. You can't know the type of object you want to create until runtime.

  2. You want to encapsulate the object creation logic if some complexity or business logic takes place, or for easier extraction and modification later on.

  3. Your system is configured with one of multiple families of products (using Dependency Injection we can easily switch the configuration in runtime).

  4. A family of products should be used together.

Structure

  • Product: declares the interface of the object the factory method will create.

  • ConcreteProduct: implements the Product interface.

  • AbstractFactory: declares the factory methods which returns the objects of type Product.

  • ConcreteFactory: implements the factory methods to return the instances of ConcreteProducts.

Note

Abstract Factory can be mentioned as a Factory of Factories, but this kind of implementation/usage is not mentioned in any of the learning sources used to create this repo (GoF Book / Source Making / Wikipedia).

Examples

Source Code

UML

// TODO

You can find the tests .

here
Example 1