Sometimes I think that Controller and Command are the same because they both encapsulate business logic. The only difference I can think of is that a command must have an execute() method with no parameters but the controller’s execute() method can accept parameters. Or am I wrong?
Sometimes I think that Controller and Command are the same because they both encapsulate
Share
It’s a bit like comparing apples to oranges. Conceptually a command is a design pattern, but the controller is a part of a meta design pattern. Which means that they are not mutually exclusive or inclusive. A controller could consist out of a command (or even a collection of commands).
There are many flavors of MVC and the controller has slightly different responsibilities in many of them, but in general the controller is strictly used to translate user input to application data (model). In most MVC flavors it’s also responsible for updating the view when the model changes.
In general controllers are longlived. They exist as long as a certain model and/or view exists, or even outlive them if they have a view/model registration system.
A command on the other hand is theoretically stateless, it does its thing and then is released for garbage collection. In reality commands can live just a little longer than the execution time of their execute method, this is to facilitate async operations more easily. But commands should ALWAYS be strictly focused on one and only one task only and when that’s finished they should disappear.