Abstract Factory pattern

An overview of the

The Abstract factory pattern is a creative design pattern that creates a series of related objects without specifying their concrete classes

The solution

The Abstract factory pattern suggests explicitly declaring interfaces for each product in the family, and then ensuring that all product variants inherit these interfaces.

The abstract factory pattern is suitable for application scenarios

  1. You can use an abstract factory if your code needs to interact with multiple related products from different families, but you don’t want to build your code based on a concrete class of the product because you don’t have access to the information in advance, or for future extensibility

    Abstract Factories give you an interface to create objects for each family of products. As long as the code creates objects through this interface, you will not generate products that are inconsistent with the type of products already generated by the application

  2. If you have a class that is based on a set of abstract methods, and its primary functionality is therefore unclear, you can consider using the abstract factory pattern in this case

    In a well-designed program, each class does only one thing, and if a class interacts with multiple types of products, consider extracting factory methods into separate factory classes or abstract factory classes with full functionality

implementation

  1. Draw a matrix with different product types and product variants as dimensions
  2. Declare abstract product interfaces for all products. Then all concrete product classes implement these interfaces
  3. Declare an abstract factory interface and provide a set of build methods for all abstract products in the interface
  4. Implement a concrete factory class for each product variant
  5. Develop initialization code in your application. This code initializes a specific concrete factory class, depending on the application configuration or current environment, and then passes the factory object to all classes that need to create the product
  6. Find all direct calls to the product constructor in your code and replace them with calls to the corresponding build method in the factory object

Advantages and disadvantages of the abstract factory pattern

Advantages:

  1. Ensures that products produced in the same factory match each other
  2. Coupling of client and specific product code can be avoided
  3. Single responsibility principle
  4. The open closed principle

Disadvantages:

  1. Because adopting this pattern requires introducing many interfaces and classes into the application, the code can be more complex than before

Pay special attention to

Essential differences between the abstract factory pattern and the factory method pattern

I personally believe that the essential difference is that the factory approach addresses one product hierarchy and one abstract product class, whereas the abstract factory addresses multiple product hierarchy and multiple product classes