Tuesday, September 3, 2024

Abstract Factory Pattern


What

Abstract Factory Pattern is another layer of abstraction over Factory pattern. It is used to create families of related objects without specifying their concrete classes. 

It provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

Who

It has four key actors as below

  1. Abstract Factory is an interface for creating a family of related objects, but it does not specify the concrete classes of the objects to be created.
  2. Concrete Factory is a class that implements the Abstract Factory interface to create a family of related objects.
  3. Abstract Product is an interface for a type of product. 
  4. Concrete Product is a class that implements the Abstract Product interface to create a specific type of product.

How

End user promotes a clear separation between the creation of product families with cars and their specifications. It has the following key steps:

  • Create an interface for the Abstract Factory with a method to create a family of related objects.
  • Create an interface for the Abstract Product with a method to describe the product.
  • Create a Concrete Factory class that implements the Abstract Factory interface to create a family of related objects.
  • Create a Concrete Product class that implements the Abstract Product interface to describe the product.
  • Create a client class that uses the Abstract Factory and Abstract Product interfaces to create and describe a family of related objects.

When

Classic real-life example of Abstract Factory pattern is GUI toolkit like button, checkbox, menu across different operating systems like Windows, Linux Mac.

Where

Though Factory and Abstract Factory look similar, it has significant differences

Factory pattern has three key elements 

  1. create objects without mentioning exact class 
  2. single product type focus with less complexity
  3. lower-level abstraction

Abstract factory pattern has the following key elements

  1. create family of related objects without specification 
  2. multiple product type focus with more complexity
  3. higher level abstraction

Reference

Code repository https://github.com/gsenthilvel/DesignCreational/tree/main/abstractfactory

No comments:

Post a Comment