2012-12-13 58 views
0

假设我们选择了整个文件并删除了它的内容。我们如何以空间高效的方式来实现这个场景的撤销操作。执行撤消操作

回答

0

你的问题有点含糊,但命令设计模式可能会帮助你。通过这种方式,可以封装命令的执行,但也可以选择调用undo()并将主体恢复到执行命令之前的状态。它经常用于应用程序中的撤销/重做堆栈操作。

举个例子,你可以有:

public interface Command{ 

    public void exec(); 
    public void undo(); 
} 

然后为每个命令你可以有:

public class DeleteContents implements Command{ 

SomeType previousState; 
SomeType subject; 

public DeleteContents(SomeType subject){ 
    this.subject = subject; // store the subject of this command, eg. File? 
} 

public void exec(){ 
    previousState = subject; // save the state before invoking command 

    // some functionality that alters the state of the subject 
    subject.deleteFileContents(); 
} 

    public void undo(){ 

    subject.setFileContents(previousState.getFileContents()); // operation effectively undone 
    } 

} 

可以存储在数据结构中的命令,并可以(如控制器)。自由调用对它们的执行和撤消。这有助于你的情况吗?

这里有一个链接: http://en.wikipedia.org/wiki/Command_pattern

+0

为了实现我们可以使用堆栈简单的撤销操作,但是如果我们想在我们做CTRL + A,然后delete.Can我们仍然使用的情况下也撤消操作叠加么? – user1846721