One of the Creational patterns is the Abstract Factory design pattern. Abstract Factory Pattern is nearly identical to Factory Pattern and is regarded as an additional layer of abstraction over Factory Pattern. Abstract Factory patterns are based on a super-factory that generates other factories.
The implementation of the abstract design pattern provides us with a framework that allows us to design objects that follow a generic pattern. Due to this, any appropriate concrete factory that is capable of producing the desired type of object is coupled with the abstract factory at runtime.
Difference between factory method and abstract factory
- The primary distinction between a “factory method” and a “abstract factory” is that the former is a single method, whilst the latter is an object.
- The factory method is simply a method that may be overridden in a subclass, however the abstract factory is an object with numerous factory methods attached to it.
- The Factory Method pattern makes use of inheritance and delegated object instantiation to a subclass.
Abstract Factory Design Pattern Benefits
- The Abstract design pattern provides a method for writing code for interfaces rather than implementations.
- The Abstract Factory design is a “factory of factories” that may be readily upgraded to handle more goods, such as adding another sub-class Laptop and a factory LaptopFactory.
- The Abstract Factory pattern is robust and avoids the Factory pattern’s conditional logic.
When to Use the Factory Method Pattern
When it is necessary to decouple a client from a specific product that it needs, utilise the Factory Method pattern. Use the Factory Method to relieve a client of the duty of creating and configuring product instances.
When Should You Use the Abstract Factory Pattern?
When clients must be separated from product classes, use the Abstract Factory pattern. This is especially handy for configuring and modifying programmes. The Abstract Factory design can also impose restrictions on which classes must be used in conjunction with others. Making new concrete factories may be time-consuming.
Things to remember
- Abstract Factory classes are typically created with Factory Methods, although they can alternatively be implemented with Prototype.
- To hide platform-specific classes, Abstract Factory can be used instead of Facade.
- Builder is concerned with building a complex object step by step. Abstract Factory emphasises a product object family (either simple or complicated). The Builder returns the product as a final phase, but the Abstract Factory returns the product immediately.
- Typically, designs begin with the Factory Method (less complicated, more adaptable, subclasses proliferate) and progress to the Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer realises where more flexibility is required.