Virtual API Method Design Pattern
The Virtual API design pattern, sometimes called the Virtual Function Table (VFT) pattern, is a valuable technique in embedded C programming for providing a common interface to different implementations of the same functionality.
Problem Statement of Virtual API Method Design Pattern:
Let’s consider a scenario where we have an existing library that provides functionality to control a motor, but we want to use a new motor control library that has a different interface. we can create an adapter to make the old library compatible with the new interface.
C
// Existing Motor Control Library typedef struct { void (*startMotor)(); void (*stopMotor)(); } OldMotorController; void oldStartMotor() { // Code to start the motor in the old library } void oldStopMotor() { // Code to stop the motor in the old library } OldMotorController createOldMotorController() { OldMotorController controller; controller.startMotor = oldStartMotor; controller.stopMotor = oldStopMotor; return controller; } // New Motor Control Library typedef struct { void (*powerOn)(); void (*powerOff)(); } NewMotorController; void newPowerOn() { // Code to power on the motor in the new library } void newPowerOff() { // Code to power off the motor in the new library } NewMotorController createNewMotorController() { NewMotorController controller; controller.powerOn = newPowerOn; controller.powerOff = newPowerOff; return controller; } // Adapter to make the old library compatible with the new interface typedef struct { OldMotorController oldController; } MotorAdapter; void adapterPowerOn(MotorAdapter *adapter) { adapter->oldController.startMotor(); } void adapterPowerOff(MotorAdapter *adapter) { adapter->oldController.stopMotor(); } // Main function demonstrating the usage of the adapter int main() { // Using the new motor control library directly NewMotorController newController = createNewMotorController(); newController.powerOn(); newController.powerOff(); // Using the old motor control library through the adapter MotorAdapter adapter; adapter.oldController = createOldMotorController(); adapterPowerOn(&adapter); adapterPowerOff(&adapter); return 0; } |
Explanation of the above Code:
In this example,
- MotorAdapter allows the old motor control library to be used with the interface of the new library without modifying the existing code of the old library.
- It contains the different type of functionality like powerON, powerOf, create New Motor Controller.
- The Virtual API Design Pattern helps in maintaining compatibility and reusability in embedded C systems.
Design Patterns for Embedded Systems in C
When working with embedded systems in C, there are so many design patterns that are particularly very useful. Many design patterns can be applied to embedded systems development in C. In this article, we will discuss design patterns for Embedded Systems in C, let’s see all of them one by one with the help of examples.
Important Topics for Design Patterns for Embedded Systems in C
- What is a Design Pattern?
- Creational Design Patterns for Embedded Systems in C
- Factory Method Design Pattern
- Object Method Design Pattern
- Opaque Method Design Pattern
- Singleton Method Design Pattern
- Structural Design Patterns for Embedded Systems in C
- Callback Method Design Patterns
- Inheritance Method Design Pattern
- Virtual API Method Design Pattern
- Other Design Patterns for Embedded System in C
- Bridge Method Design Pattern
- Concurrency Method Design Pattern
- Spinlock Method Design Pattern
- Mutex Method Design Pattern
- Conditional Method Design Pattern
- Behavioral Method Design Pattern