Our Latest News

Embedded C Design: Explanation of Abstract Factory Pattern Programming

Embedded C Design: Explanation of Abstract Factory Pattern Programming

1 – What is the Abstract Factory Pattern?

In the previous two articles, we talked about two types of factory patterns: the Simple Factory pattern and the Factory Method pattern, both of which have their own advantages, disadvantages, and applicability scenarios.

This article continues with the last factory pattern, the Abstract Factory Pattern. (Abstract Factory Pattern)

The abstract factory pattern is a pattern structure that provides an interface for accessing classes to create a set of related or interdependent objects, and the accessing class can get different levels of products of the same family without specifying the specific class of the product they want.

As the previous two factory patterns say, you are the CEO of a factory that just started with one factory (simple factory pattern) that produces different products depending on the season.

Then you make a lot of money and build different factories (factory method pattern) to produce different products.

Then today, you simply start your own engineering and construction team, and build whatever factory you like (abstract factory model).

For example, factory A, it is not only the production of LCD, it also produces with the LCD together with the touch panel, and then assembled into a touch display module, B factory is the same thing.

Then, the LCD and touch panel of factory A, forming a product family within the same company, and the touch panel of factory A and factory B, is the same product level of different companies.

In layman’s terms, the abstract factory pattern is mainly on the factory for further abstraction, that is, this “abstract out of the factory” is used to produce “factory”, it does not produce a specific product, the specific products produced by it.

The concrete product is produced by the “product factory”, and the “product factory” does not only produce a product, but also a variety of products.

In other words, in the abstract factory method, there will be abstractions of A and B factories, abstractions of the same product level, and abstractions of the same product family.

2 – When to use abstract factory pattern?

In the factory method pattern, a product family corresponds to a factory class, for example, LCD product corresponds to lcd_factory, and if a new product family is added, for example, a touch product, a new factory class corresponding to that product is added to touch_factory.

In the abstract factory pattern, a factory class is used to build several different types of products, for example, a_factory can build both LCD products and touch products.

For this kind of scenario, for example, LCD should be bound with touch panel to form a touch screen module, and this touch screen module is provided by different suppliers respectively.

For example, company A to provide a good touch screen module (including LCD and touch panel), and with the touch screen module provided by company B is not compatible, but we are in the application layer, to adapt the touch screen module provided by both company A and company B, this time you can use the abstract factory pattern.

Because we design in the application layer, is not concerned with the initialization of the module itself and the specific operation, we want to use the same interface to adapt the touch screen modules provided by different manufacturers in the application layer.

3 – How to use the abstract factory pattern?

Concrete scenario: For example, there are two factories, factory A and factory B, they have the ability to produce both LCD and Touch touch panel, and, the LCD produced by factory A can only be adapted to the Touch panel produced by factory A, the products of the two factories A and B, are not adaptable to each other.

Then in the software design, you need to abstract the LCD product and Touch touch panel products (declare a structure, such as: struct lcd and struct touch), but also the abstraction of the production capacity of the two factories A and B, such as the declaration of the structure structlcd_touch_abstract_factory.

1, first create two header files, used to declare structlcd and struct touch these two types of products, said the two products for the concrete abstraction, note that this is only a product abstraction, not a concrete product instances, in order to concise expression, these two structures only implement a simple printing function, the specific code is shown in the following figure.

2, then create the structure structlcd_touch_abstract_factory about the abstract factory, this structure represents an abstract factory, note that this is only the abstraction of the factory, not an instance of the factory, you need to use this structure to define the structure variables, in order to be considered an instance of the factory, the code is shown in the following figure.

3, with the specific abstraction of the two products LCD and touchpad Touch, then the two factories can be produced for these two products respectively for the specific instantiation, that is, for the A factory LCD and touchpad Touch, you need to have the corresponding products a_lcd and a_touch, B factory is the same reason, the code is shown below.

4, to A factory production of a_lcd for example, in a_lcd.h, the main declaration of a_lcd class, this class inherits from the lcd.h inside the parent class, and provides the constructor and destructor to a_lcd the creation and destruction of this product, a_lcd.h code, as shown below.

5, in a_lcd.c this file, mainly the specific implementation of the constructor and destructor, and is to achieve the specific functions of a_lcd this product, only a simple print function is implemented here, the specific code is shown below.

6, for A factory production of a_touch products, as well as for B factory production of b_lcd and b_touch, the same reason, you can use the same idea to understand by example, here limited to space, will not list the specific code of a_touch, b_lcd, b_touch.

7, although there are specific product instances (a_lcd, a_touch, b_lcd, b_touch), you also need to have specific factory instances in order to build the product, the abstraction of the factory has been carried out, then, based on the previous factory abstraction, you can create a_factory and b_factory these two factory instances, as shown in the following figure.

8, to analyze the example of A factory a_factory (B factory can be cited), first create a a_factory.h header file, declare a structure struct a_factory, this structure inherits from the previous abstract factory lcd_touch_abstract_factory_t, and provide A factory constructor and destructor, as shown in the following figure.

9, in the a_factory.c file inside, is the concrete implementation of the factory’s product manufacturing capabilities (because the abstract factory class inside, these capabilities are encapsulated), for example, create LCD and destroy LCD, create Touch and destroy Touchpad Touch, and then in the factory’s constructor and destructor inside, respectively, initialize the factory abstract class interface.

10, so far, about the abstract factory pattern inside the four roles: abstract factory, concrete factory, abstract products, concrete products, have all been achieved, the specific logic of the relationship, you can carefully read the source code.

11, in the business application layer of the code, we only need to use the A factory and B factory constructor to create a specific factory, and then the creation of a specific factory, assigned to the generic factory model, a series of operations can be based on this generic factory model, the specific code is shown in the figure below.

    GET A FREE QUOTE

    FPGA IC & FULL BOM LIST

    We'd love to

    hear from you

    Highlight multiple sections with this eye-catching call to action style.

      Contact Us

      Exhibition Bay South Squre, Fuhai Bao’an Shenzhen China

      • Sales@ebics.com
      • +86.755.27389663