Page 1 of 1
Is Undo in the Controller Layer?
Posted: Sat Oct 16, 2010 7:14 pm
by Jonah Bron
Hello, world!
I'm writing a desktop application (mentioned in my thread about a
C++ forum). I'm doing my best to implement design patterns where applicable, and so far I think I've been doing pretty well. So far, it has something of a Model-View-Controller structure. The app will support Undo. My question is, where does Undo belong? It seems like it should be in the Controller. Should I implement it as an independent layer in the controller, or just notify it of commands as they occur?
Confession: It's written in Java, not PHP. I realize this section is titled "PHP - Theory and Design", but these concepts could apply to basically any language.
Basically, I just want to get these concepts cleared up in my brain.
Thanks
Re: Is Undo in the Controller Layer?
Posted: Sun Oct 17, 2010 12:32 am
by Christopher
Undo is like any other action. It's handled by the Controller, the actual undoing is done by a Model or Models, and the View is redisplayed or updated.
Re: Is Undo in the Controller Layer?
Posted: Sun Oct 17, 2010 10:06 am
by mkz
In the context of handing undo actions, the
Command Pattern often comes up. It means you have a command object that encapsulates every command and its parameters, and which makes available an "undo" method that undos the action. Perhaps this can help organize things for you.
Re: Is Undo in the Controller Layer?
Posted: Sun Oct 17, 2010 11:32 am
by Jonah Bron
Christopher wrote:Undo is like any other action. It's handled by the Controller, the actual undoing is done by a Model or Models, and the View is redisplayed or updated.
Oh yes, of course. It's just another action, isn't it? Isn't it interesting that the link you gave in
this thread apparently applies here.
mkz wrote:In the context of handing undo actions, the
Command Pattern often comes up. It means you have a command object that encapsulates every command and its parameters, and which makes available an "undo" method that undos the action. Perhaps this can help organize things for you.
I'll look at that pattern, thanks. It looks like it may be similar to (or the same as?) the Event Sourcing pattern.
So, would I use one of those patterns to organize the undoing in the model itself?
Re: Is Undo in the Controller Layer?
Posted: Sun Oct 17, 2010 1:00 pm
by josh
Christopher wrote:Undo is like any other action. It's handled by the Controller, the actual undoing is done by a Model or Models, and the View is redisplayed or updated.
+1.
If I had to say where it "is" (picking just one place) I'd say the model.
Re: Is Undo in the Controller Layer?
Posted: Sun Oct 17, 2010 5:34 pm
by Jonah Bron
How would the controller keep track of which model to send the undo command to? Should I create another model that holds how the program state changes?
Re: Is Undo in the Controller Layer?
Posted: Sun Oct 17, 2010 6:00 pm
by josh
Jonah Bron wrote: Should I create another model that holds how the program state changes?
That's what the command pattern is, you create a table & object that are responsible for tracking the information needed to undo an action.
Re: Is Undo in the Controller Layer?
Posted: Tue Oct 19, 2010 8:10 pm
by Jonah Bron
Good, I totally understand it now. That's a very cool pattern. Thanks very much.
In case anyone is interested, I found a great tutorial on implementing the Command pattern:
http://mattberther.com/2004/09/16/using ... ctionality