In a document editor application, is it better to implement the command pattern as a layer on top of the object model, or to deeply integrate it into the object model?
If commands are a layer on top, they would manipulate the object model using its regular getters and setters.
- Pros: Commands are higher level, representing user actions that may involve various low-level model mutations. The model doesn't have to know about commands.
- Cons: Object model mutators should have as little side effects as possible so that the commands know how to undo their effects.
If commands are deeply integrated, every object model mutator would automatically record an undo action for their effect.
- Pros: Easier to guarantee that every mutation is correctly undoable.
- Cons: Commands are lower-level than user actions, so there needs to be a begin/end signal to aggregate any model mutation into the current user-level action for undoing.
Or maybe a third option is to use the observer pattern to listen for low-level model mutations like property changes and record them into undoable commands, aggregated by user action?
Most command pattern articles online don't mention these architectural considerations. I'm looking for real-life experiences, open source software examples that support one of these approaches or articles/books that deal with the topic more in-depth.