If you are passing different type of commands, Invoker
is useful. You can use same Invoker for execution of different concrete commands. On a different node, tagging Receiver
with ConcreteCommand
instead of Invoker
allows loose coupling. The Receiver
may change the name of the method (e.g. switchOn to swithcOnTV) as in this example:
Related post: Using Command Design pattern
To understand the purpose of Invoker
,I would like you to refer this article on Restaurant & Car Service centre use cases.
The waiter (Invoker
) takes the order from the Customer
on his pad. The Order
is then queued for the order cook and gets to the cook (Receiver
) where it is processed.
The Client is the Customer
. He sends his request to the Receiver
through the waiter, who is the Invoker
. The waiter encapsulates the command (the order in this case) by writing it on the check and then places it, creating the ConcreteCommand
object which is the command itself.
The Receiver
will be the cook that, after completing work on all the orders that were sent to him before the command in question, starts work on it.
Another noticeable aspect of the example is the fact that the pad for the orders does not support only orders from the menu, so it can support commands to cook many different items.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…