Implementing the Strategy Pattern
In this lesson, we will explain the implementation of the Strategy pattern in the C language.
The open-closed principle
Although mainly seen in the context of object-oriented literature, the open-closed principle defines properties attractive in the context of C too. The principle is summarized as “Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.” (Martin)
According to the open-closed principle:
- Extending the behavior of an ideal module is achieved by adding code instead of changing the existing source.
- Following this principle minimizes the risk of introducing bugs in existing, tested code and typically raises the quality of the design by introducing loose coupling.
- Unfortunately, it is virtually impossible to design a module so that it is closed against all kinds of changes.
- Even trying to design software in such a way would overcomplicate the design far beyond suitability.
- Identifying which modules to close and what changes to close them against requires experience and a good understanding of the