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
- 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.
- Concrete Factory is a class that implements the Abstract Factory interface to create a family of related objects.
- Abstract Product is an interface for a type of product.
- 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
- create objects without mentioning exact class
- single product type focus with less complexity
- lower-level abstraction
Abstract factory pattern has the following key elements
- create family of related objects without specification
- multiple product type focus with more complexity
- higher level abstraction
Reference
Code repository https://github.com/gsenthilvel/DesignCreational/tree/main/abstractfactory
No comments:
Post a Comment