# 전략 패턴

---

전략 패턴은 동적으로 알고리즘을 선택하고 교체할 수 있는 유연성을 제공한다. 전략 객체는 동일한 인터페이스를 구현하며, 전략을 변경하려면 해당 객체를 교체하기만 하면 된다.

```
class SortStrategy {
  sort(array) {
    throw new Error('sort 가 구현되지 않았습니다.');
  }
}

class BubbleSortStrategy extends SortStrategy {
  sort(array) {
    // 버블소트 구현
  }
}

class MergeSortStrategy extends SortStrategy {
  sort(array) {
    // 머지소트 구현
  }
}

class QuickSortStrategy extends SortStrategy {
  sort(array) {
    // 퀵소트 구현
  }
}

// Define context class that uses a strategy object
class SortContext {
  constructor(strategy) {
    this.strategy = strategy;
  }

  setStrategy(strategy) {
    this.strategy = strategy;
  }

  sort(array) {
    this.strategy.sort(array);
  }
}

const context = new SortContext(new BubbleSortStrategy());
context.sort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]);

context.setStrategy(new MergeSortStrategy());
context.sort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]);

context.setStrategy(new QuickSortStrategy());
context.sort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]);

```

For the site tree, see the [root Markdown](https://slashpage.com/develop.md).
