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
Is Undo in the Controller Layer?
Moderator: General Moderators
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Is Undo in the Controller Layer?
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.
(#10850)
Re: Is Undo in the Controller Layer?
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.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Is Undo in the Controller Layer?
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.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.
I'll look at that pattern, thanks. It looks like it may be similar to (or the same as?) the Event Sourcing pattern.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.
So, would I use one of those patterns to organize the undoing in the model itself?
Re: Is Undo in the Controller Layer?
+1.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.
If I had to say where it "is" (picking just one place) I'd say the model.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Is Undo in the Controller Layer?
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?
That's what the command pattern is, you create a table & object that are responsible for tracking the information needed to undo an action.Jonah Bron wrote: Should I create another model that holds how the program state changes?
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Is Undo in the Controller Layer?
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
In case anyone is interested, I found a great tutorial on implementing the Command pattern:
http://mattberther.com/2004/09/16/using ... ctionality