2016-07-15 3 views
2

실행 취소/다시 실행 기능이있는 명령 패턴과 관련하여 문제가 있습니다. 간단한 문제는 내 기록이 꽉 찼을 때 기록에서 가장 최근에 사용하지 않은 명령을 제거하고 실행시 새 명령을 추가하려고합니다. 정말로에서실행 취소/다시 실행을 사용하여 명령 패턴의 기록을 변경 하시겠습니까?

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(); 
} 

의심 현재 명령 지수가 2 남아 있고 인덱스 0에 유일한 명령 1로 이동하기 때문에이 잘못된 경우 절 :

나는 나의 교수에서이 코드를 얻었다. 그러나 그는 이것이 갈 길이라고 말했습니다. 내가 뭘 놓치고 있니?

답변

관련 문제