Is there a difference between a message handler and开发者_如何学Python a command handler? Or is there a difference between a command and a message?
Edit: There is 3rd candiate called command message ... ugh.
Actually, the difference is not in structure nor implementation, it is rather a conceptual one - both of these structures are to represent different concepts.
Message - a structure that transports some kind of information
Command - a structure that triggers some kind of action
That said, a Command is a kind of Message, as it transports information about action to invoke at least (and in most cases additional parameters as well). In terms of implementation, both MessageHandlers and CommandHandlers look very similar and depend on what do you want to do with the information.
Examples:
UserRegisteredMessage (userName) - a message that informs about registered used in a system, a handler can for display this information on UI
RegisterUserCommand(userName) - a command that instructs our system to register a user, this may involve some kind of additional action (like checks for uniqueness) and can can fail.
Mostly semantics, and where the pattern is placed, in my experience. A message may or may not have side-effects, where a command implies that it will. Also, I think command pattern is intended as a ui pattern on a client (generally, though not always), where the message pattern implies the potential for a network hop.
Also, in most implementations, a message is more loosely coupled. You send a message on a channel which may or may not be listened to. Command (usually, not always) is a more direct relationship at execution time.
精彩评论