In the Command pattern, what's the difference between the Invoker and the Receiver, and why does that split matter?
The Invoker is the object that triggers a command — a button, a menu item, a scheduled task runner — and its only job is calling execute() on whatever command it's currently holding, with zero knowledge of what that command actually does. The Receiver is the object that holds the real logic and actually performs the work, like a TextEditor's makeBold() method. The Command implementation sits between them, bound to one specific receiver method. This split matters because it lets the Invoker be written once and reused for literally any action: a GUI framework's Button class never needs to know about TextEditor, PhotoEditor, or any other receiver, because it only ever depends on the shared Command interface, not on any concrete receiver type.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response