11

Hi I'm about to add new functionality to application which I'm currently writting. I need to write a undo/redo fnctionality. However 90% of our application is ready and I don't know what is the best way to implementing this functionality without affectig(too much ) code which has been already created.

pieere
  • 141
  • 1
  • 4
  • We would have to be able to see some code or you would have to describe what your program was in order for us to help. – Andrew M Oct 19 '10 at 23:16
  • 2
    Considering you didn't even tell us what kind of application it is (web, silverlight, desktop, etc.) or what genre it is (game, CRUD, social networking, etc.) there is really nothing for us to go on. – tster Oct 19 '10 at 23:23
  • This is a WPF application which allows user to draw diagrams. User can add different block types, connect them move and resize. He also can assing some properties to block - name,dates etc. – pieere Oct 20 '10 at 17:07

3 Answers3

8

There aren't many details here. However, Undo/Redo functionality is typically handled via some variation of the Command Pattern. Depending on your architecture, this could be a simple reworking of your basic functionality into "commands", or a major overhaul.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    This is how you do it. Once everything your program does is funneled through instances of "commands" then you're 80% there. – Detmar Oct 19 '10 at 23:25
  • 3
    If the original architects/developers didn't have this in their minds when they first started designing the system, I'm leaning toward this being a major overhaul! – tster Oct 19 '10 at 23:25
  • @tster: It typically is a pretty major reworking - though it depends on how well architected the app is to begin with. – Reed Copsey Oct 19 '10 at 23:26
8

As Reed Copsey says the most common pattern to implementing do/redo is Command Pattern. The basic idea is to implement actions as commands that implemets some interface like this:

public interface ICommand  { public void Execute(); public void Undo(); }

Then You have a class (Control) that executes all the commands as a whole, such class must be composed by a group of commands, the when you execute the commands each command is pushed inside a Stack (by means the push() method), in the case you want to undo the actions then you take every element from the Stack (by means of pop() ) an executes its Undo() method.

public class Control {  
    private ArrayList<ICommand> commands  = new ArrayList<ICommand>();  
    private Stack<ICommand> stack = new Stack<ICommand>();  

    public Control() {      
        commands.add(new Command1());
        commands.add(new Command2());       
        commands.add(new Command3());       
    }

    public void Execute() {
        for(int index=0; index<=command.size(); index++) { command.Execute(); stack.push(command);}
    }
    public void Undo() 
    {
        while (!stack.empty()) {
            ICommand command = (ICommand)stack.pop();
            if (command != null) { command.Undo(); }        
        }       
    }   
}

Note: this is a very simple code, only for trying to clarify the ideas behind the Command Pattern.

link text

Renaud Bompuis
  • 16,596
  • 4
  • 56
  • 86
ArBR
  • 4,032
  • 2
  • 23
  • 29
-1

CSLA.NET is a Business Logic Library that includes built in undo/redo functionality. I think it's even implemented through an interface, so it should be "minimally invasive". They describe it better than I can here.

Joel B
  • 12,082
  • 10
  • 61
  • 69