My New Favorite Pattern for Writing Simple Code
Several years ago, I wrote an article called “My Top Four Patterns for Writing Simple Code.” Five years after writing that post, I still stand by those patterns. I use each of them almost every day.
I’ve recently been discovering a new pattern that I wanted to share as a follow-up/extension to that original post. I’ve found it especially effective in building and designing event-driven architectures. It isn’t a new pattern by any means, but I’ve been discovering a few ways to make it much more useful than it might seem at first.
Executors and Commands
The pattern I’ve been discovering is most commonly known as the Command Pattern. This pattern’s core idea is to turn actions into data and communicate those actions between components using a unified interface. This allows us to decouple an action/message from the sender and receiver, which makes it easy for components to communicate without knowing each other’s implementation details. Additionally, the thing being sent or executed is defined by an interface, allowing multiple implementations to define their version of that “thing.”
In the command Command Pattern language, we have three main components or objects:
Receiver
: the concrete action to process or actor that needs to be called. It could be…