Is Undo in the Controller Layer?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Is Undo in the Controller Layer?

Post 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
User avatar
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?

Post 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.
(#10850)
mkz
Forum Newbie
Posts: 11
Joined: Tue Oct 05, 2010 10:37 am

Re: Is Undo in the Controller Layer?

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Is Undo in the Controller Layer?

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Is Undo in the Controller Layer?

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Is Undo in the Controller Layer?

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Is Undo in the Controller Layer?

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Is Undo in the Controller Layer?

Post 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
Post Reply