Maintaining and extending objects with the OCP
Building on the groundwork laid by the SRP, we will now dive into the OCP. The OCP advocates that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
This principle propels us toward a design where the behavior of a class can be extended without altering its existing code base, thus mitigating the risk of introducing bugs into a stable system. Applying the OCP enables us to add new functionalities or respond to changing requirements with minimal disruption to the existing code, a common necessity in the dynamic environment of game development.
Understanding how to apply the OCP in practice involves recognizing the mechanisms available for extending a class’s behavior without modifying its source. Polymorphism stands out as a key strategy in that sense. We do that all the time in Godot Engine by extending nodes’ behavior. The code base is already set and, supposedly...