G2Labs Grzegorz Grzęda
Factory Method design pattern
May 12, 2023
The Factory Method
is just an implementation of the Template Method
but for building objects.
Let us consider an IRL example, I’ve recently coded. Here’s the story:
I was building and HD44780 LCD driver. I needed some hardware abstraction, so that I could test the rest of the software without any running hardware. Also, I didn’t know if I would bit-bang
my way through, or use some funky I2C IO expander as a proxy.
Depending on my project configuration, I needed an LCD driver:
The actual drivers in question would be:
Each one behaving totally differently but giving the same end result: controlling the HD44780 (or pretending) in a proper way.
Now I needed an entity that would deliver me a proper driver, depending on the given project configuration.
Now defining classes:
I could easily generate drivers based even on #ifdef
’s"
|
|