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]);