Yesterday, I somewhat implemented the Chain of Responsibility design pattern in C++. This was because I grew tired of writing code that constantly called into some C library, checked an error code, and continued on to call another function IF the error was not present. This lent itself naturally (in my opinion) to a Chain of Responsibility pattern, though I may not have implemented it perfectly. Chain of Responsibility This design pattern can be boiled down to one idea: a chain of handlers working together to form a “super” handler of sorts.
Working on a project that I currently have, I’ve found myself needing some mutual exclusion in dealing with some hardware devices. I could have gone the singleton route, where I only keep one device representation in memory at a time, but the internet and stack overflow have both told me how I am oh so wrong for wanting that. Thinking then about how I can support multiple devices in the future, while maintaining a single-device for now in a thread-safe manner, I’m forced to use mutexes to protect critical sections of code.