Abstract factory pattern – An overview and how to use it

Tram Ho

Concept

Abstract factory is a pattern for object-oriented design in software, it provides an interface class that creates related objects without specifying what specific classes are at design time. It is classified as design pattern creational design.

Source: refactoring.guru

Background

Suppose we have a fashion factory with many workshops. The other is in charge of shoe production, the other is in the production of skirts, the other produces hats, etc. The requirement is that we need to create a factory that can accommodate most of the changing tastes of users and tastes. schools, like in summer, they will produce model A, in winter, they will produce model B for each product line of shoes, skirts, clothes … Provided that there is no need to invest more in machinery and labor or rearrange. personnel apparatus is very expensive and time consuming. This is similar to the design of software systems, in this article I assume this software is such a fashion factory.

With that in mind we need a way to create a separate product line. For example, in summer there are summer shoes, summer skirts and in winter there are winter shoes and skirts.

Solution

  • The first thing Abstract Factory pattern proposes to explicitly declare interfaces for each product type separately for each product line (eg shoes, dresses, shirts, hats, …). Next, you need to make all product variations according to interfaces. For example, all variations of the shoe will implement Ishoe interface, variations of the skirt will implement the IDress interface, .. You can imagine as shown below:

  • Next, you declare Abstract Factory , an interface with a list of methods to create all products in the same product line (the line here is interpreted as summer and winter models).

Now, how with the product variants? For each product line variant, we create a separate factory class based on the AbstractFactory interface. A factory class is a class that returns the product of a particular type. SummerFashion for example, will only create SummerShoe , SummerDress , and so on.

Structure

Source: refactoring.guru
  • Abstract Products : Declare interfaces for separate types but the related products will form a product line.
  • Concrete Products : These are various implementations of the abstract product grouped by variations. Each abstract product ( Shoe , Dress ) must be implemented in all variants (Summer, Winter).
  • Abstract Factory : Interface declares a set of methods for creating each abstract product
  • Concrete Factories : Implement methods to create products of abstract factory. Each concrete factory is responsible for a variant of the product and only creates products of this variation.
  • Client : Although concrete factories make concrete products, the return styles of product creation methods must return their respective abstract products. In this way the client code uses a factory that is not associated with a variant of the product it receives a factory. Clients can work with any concrete factory (product variation), as long as it communicates with their objects through abstract interfaces.

Application

  • Use Abstract Factory when your code needs to work with variations of related products, but don’t want to depend on the concrete classes of those products (They may not be known in advance or you simply want to extend in future).

Abstract Factory provides you with an interface for creating objects from each class of the product line. As long as your code creates objects through this interface, you don’t have to worry about creating the wrong variation of the product (wrong means it doesn’t match any of the products created in your app).

  • Consider using Abtract Factory when you have a class with a set of Factory Methods .
  • In a well-designed program, each class is responsible for only one thing. When a class deals with multiple types of products, it should separate the factory methods into a factory class or in other words, use Abstract Factory.

Advantages and disadvantages

Advantages:

  • You can ensure that the products you get from the factory are compatible with the others.
  • You avoid sticking between concrete products and client code.
  • Single Responsibility Principle . You can separate the code to create the product to one place, making the code easy to support
  • Open / Closed Principle . It’s easy to add new product variations without disrupting old code

Defect:

  • Code can become more complex than necessary if more interfaces and classes are added.

How to implement

  • Mapping a matrix of product types and variants of these products.
  • Declare abstract product interfaces for all types of products. Next, create all concrete product classes to implement these interfaces.
  • Declare abstract factory interface with a set of creation methods for all abstract products.
  • Implement a set of concrete factory classes, one class for each product variant.
  • Create the code to initialize the factory somewhere in the application. It should instantiate one of the concrete factory classes, depending on the application configuration or current environment. Pass this factory object to all classes where the product is built.
  • Go through the code and find all direct calls to the product constructor. Replace them with the creation method appropriate for the factory object.

Demo with C # code

Create product Interfaces, in which each product has 2 methods GetName() and GetModel()

Create Abtract Factory class, in which the methods to create different product lines

Create variants of Product (concrete product) implement product interface:

Create variations of each product line:

Implemnt client code:

Executed in the main program:

Results of running the program :

Link full demo code: https://github.com/quanghiepth86/abstract-factory-csharp-demo

Discuss

In this article I have briefly introduced about the Abtract Factory pattern, its advantages and disadvantages and how to use it. I think this is a pattern that is used quite widely for object-oriented applications. Hope the article will help you when applying in practice. Thank you for watching the article.

Reference source: https://refactoring.guru/design-patterns/abstract-factory

Share the news now

Source : Viblo