G2Labs Grzegorz Grzęda
Open/Close principle
June 2, 2023
In software architecture its all about happy clients. Happy clients gets from working software and well delivered requirements. When clients change their minds at the end its up to us, The Coders, to how we respond to those changes.
A good piece of software is such that is easy to change. When requirements update, so should the architecture and every module.
Adhering to the Open-Closed Principle provides such ‘plasticity’. The piece of software is at the same time:
- open for extension,
- closed for modification.
Closed to modification
By defining functionality of a module, we tell the world what it is capable of doing. Let’s consider a CHIP8 emulator to be written in C++. We can create an interface for every command to be executed:
Every command, while loaded is executed in the same way. There is no way a command would be treated in another way.
Open to extension
Most of commands advance the Program Counter
by two bytes:
Also, lets consider, that we want to add a logging feature to each command. We could do that on many different ways - by an template method, by a visitor or observer Design Pattern.
We may now extend the functionality of the ANormalCommand
:
|
|
Summary
The OCP
is a guard, reminding us, that software architecture is about proper decoupling of modules. Besides stiff and rigid relations, the software also should be easy to modify/update. Enabling openness to extension and closeness for modification we achieve both: sustaining current architecture and extending modules for new functionality.