OPEN HYPER STEP
← 목록으로 (TypeScript)
TS · 33 / 34
ts
CHAPTER 33 / 34
읽기 약 2분
FUNCTION

TypeScript로 커맨드 패턴 구현


핵심 개념

커맨드 패턴으로 실행 취소/재실행 기능을 구현합니다. Command 인터페이스로 execute와 undo를 정의합니다. 히스토리 스택으로 작업 기록을 관리합니다. 커맨드 시스템을 지우며 실행 취소 기능이 어떻게 사라지는지 확인하십시오.

코드 분석
TS📋 코드 (52줄)
<div style='font-family:monospace; padding:16px; background:#0d1117; color:#e8e8e8;'>
<div style='color:#10b981; margin-bottom:12px;'>COMMAND PATTERN</div>
<div id='cmd-state' style='font-size:13px; margin-bottom:8px; color:#10b981;'>UNITS: []</div>
<div id='cmd-history' style='font-size:10px; color:#555; margin-bottom:8px; min-height:30px;'></div>
<div style='display:flex; gap:6px; flex-wrap:wrap;'>
  <button onclick='executor.execute(new AddUnitCmd("T-800"))' style='background:#10b981; color:#080808; border:none; padding:5px 10px; cursor:pointer; font-family:monospace; font-size:9px;'>ADD T-800</button>
  <button onclick='executor.execute(new AddUnitCmd("T-1000"))' style='background:#f59e0b; color:#080808; border:none; padding:5px 10px; cursor:pointer; font-family:monospace; font-size:9px;'>ADD T-1000</button>
  <button onclick='executor.undo()' style='background:#333; color:#888; border:none; padding:5px 10px; cursor:pointer; font-family:monospace; font-size:9px;'>UNDO</button>
  <button onclick='executor.redo()' style='background:#1a1a1a; color:#555; border:none; padding:5px 10px; cursor:pointer; font-family:monospace; font-size:9px;'>REDO</button>
</div>
<script>
  let units = [];

  class AddUnitCmd {
    constructor(unit) { this.unit = unit; }
    execute() { units.push(this.unit); }
    undo() { units = units.filter(u => u !== this.unit); }
  }

  class CommandExecutor {
    constructor() { this._history = []; this._redoStack = []; }
    execute(cmd) {
      cmd.execute();
      this._history.push(cmd);
      this._redoStack = [];
      this._render(`[EXECUTE] ${cmd.unit || '?'} 추가`);
    }
    undo() {
      const cmd = this._history.pop();
      if (!cmd) return;
      cmd.undo();
      this._redoStack.push(cmd);
      this._render(`[UNDO] ${cmd.unit || '?'} 제거`);
    }
    redo() {
      const cmd = this._redoStack.pop();
      if (!cmd) return;
      cmd.execute();
      this._history.push(cmd);
      this._render(`[REDO] ${cmd.unit || '?'} 재추가`);
    }
    _render(msg) {
      document.getElementById('cmd-state').textContent = `UNITS: [${units.join(', ')}]`;
      const el = document.createElement('div');
      el.textContent = msg;
      document.getElementById('cmd-history').prepend(el);
    }
  }

  const executor = new CommandExecutor();
</script>
</div>

AI 프롬프트
🤖 AI에게 잘 물어보는 법 — 모델·전략별 프롬프트
Claude

무료: Sonnet 4.6 / Pro $20/mo: Opus 4.6

이 TS '커맨드 패턴' 코드에서
타입 에러·any 남용·타입 안전성 누락을 찾아
엄격한 타입으로 리팩토링해줘.
ChatGPT

무료: GPT-5.5 / Plus $20/mo: GPT-5.5 Pro

'커맨드 패턴'를 실무에서 사용하는
패턴 3가지를 비교표로 보여주고
각각의 적합한 케이스를 알려줘.
Gemini

무료: 2.5 Flash / Pro $19.99/mo: 3.1 Pro

이 TS '커맨드 패턴' 관련 코드 전체의 타입 구조를
분석하고 제네릭·유틸리티 타입으로
개선할 포인트를 정리해줘.
Grok

무료: Grok 4.1 / SuperGrok $30/mo

TypeScript '커맨드 패턴'를 꼭 써야 하는지
개발 속도 vs 안전성 관점에서
솔직하게 평가해줘.

⭐ 이것만 기억하세요
TypeScript로 커맨드 패턴 구현 이 3가지만 확실히 잡으세요
1.작업을 직접 실행하면 실행 취소(undo)가 불가능하고, 작업 기록을 남길 수 없습니다
2.각 작업을 execute()와 undo() 메서드를 가진 커맨드 객체로 캡슐화하고, 히스토리 스택으로 관리합니다
3.다음 챕터에서 TypeScript 트랙의 모든 개념을 종합하는 최종 미션에 도전합니다


공유하기
진행도 33 / 34