Command Design Pattern — 3 Minute Series

Invoker -> Command -> Executor

Elric Edward
Sep 24, 2022
Photo by Athul Cyriac Ajay on Unsplash

_00 / Concept

Command Design Pattern makes your method’s behavior switchable and decouples two classes. By defining the command in the run time, your Invoker can execute different behaviors.

_01 / Key Roles

Invoker and Receiver (Executor). The Invoker will define a command in the runtime and bind the command to a specific method, then call the command’s execute method. At the same time, the command will hook another Receiver and use the execute function to call the Receiver to do something.

_02 / Trade-offs

🟢 Loose coupling, you can use the same invoker interface but control different devices. It’s widespread in everyday life.
🟢 Follow the OOP principle very well. Open to extending and close to modification.
🔴 A lot of codes probably will cause maintenance issues.

--

--