【发布时间】:2016-11-18 12:53:24
【问题描述】:
我遇到了关于具有撤消/重做功能的命令模式的问题。简单的问题是,当我的历史记录已满时,我想从历史记录中删除最近最少使用的命令,并在执行时添加新命令。
我从教授那里得到了这个代码 sn-p:
public class CommandHistory implements CommandInterface{
private static final int MAX_COMMANDS = 2;
private Command[] history = new Command[MAX_COMMANDS];
private int current = -1;
@Override
public void execute(Command command) {
current++;
if (current == MAX_COMMANDS){ // if full, then shift
for (int i = 0; i < MAX_COMMANDS - 1; i++){
history[i] = history[i+1];
}
}
history[current] = command;
history[current].execute();
}
确实怀疑 if 子句 是不正确的,因为当前命令索引仍然是 2,并且只有索引 0 处的命令被转移到 1。但他说这是要走的路。我错过了什么?
【问题讨论】:
标签: java arrays history undo command-pattern