Examples on the Internet, like the example here, confused me about the Command
pattern In most examples the concrete command directly calls one of receiver's methods. Is this the only responsibility of a concrete command? Where does the actual business logic belong? In the execute()
method of concrete command or in some method on the receiver?
Another question is if we want to implement multi threaded command pattern, our thread pool should receive commands from Invoker
and run the execute()
methods of concrete commands? Is my understanding correct?
Either is possible, and I've implemented Command with concrete commands that contain the implementation of the command in simple applications, but, in typical large applications, commands need to do nothing more than convey the type of command and any arguments. It's up to the receiver to contain or delegate to the implementation of the behavior.
This is because the client has to depend on concrete commands. If the concrete commands in turn require complex dependencies to implement behavior, the client indirectly depends on those complex dependencies too, which makes for an unstable system. Instead, concrete commands should have no complex dependencies, so that clients can depend on them without fear.
[I ignored your second question. Please move it to a new question so we can answer it separately.]