策略模式(StrategyPattern):定义一系列算法,将每一个算法封装起来,并让它们可以相互替换。策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy)。
策略模式是一种对象行为型模式。
/**
* 排序策略父类
*/
class Stratege {
sort() {
throw new Error('顶级父类的sort不能直接调用')
}
}
/**
* 冒泡排序
*/
class BubbleStratege extends Stratege {
sort([...arr]) {
for (let i = 0; i < arr.length; i++) {
for(let j = 0; j < arr.length; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
}
/**
* 选择排序
*/
class ChoosenStratege extends Stratege {
sort([...arr]) {
for (let i = 0; i < arr.length; i++) {
for(let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
}
/**
* 插入排序
*/
class InsertStatege extends Stratege {
sort([...arr]) {
for(let i = 1; i < arr.length; i++) {
let value = arr[i];
let j = i - 1;
while(j >= 0 && arr[j] > value) {
arr[j + 1] = arr[j];
j --;
}
arr[j + 1] = value;
}
return arr;
}
}
module.exports = {
BubbleStratege,
ChoosenStratege,
InsertStatege,
}
/**
* 上下文
*/
module.exports = class Context {
constructor(arr) {
this.arr = arr;
}
setSort(stratege) {
return stratege.sort(this.arr);
}
}
const Context = require('./Context');
const {
BubbleStratege,
ChoosenStratege,
InsertStatege,
} = require('./Stratege')
function main() {
//生成随机数组
let arr = Array.from({length: Math.ceil(Math.random() * 20)}, _ => Math.floor(Math.random() * 100));
console.log(arr, '原始数组')
//上下文
const context = new Context(arr);
//冒泡排序策略
let arr1 = context.setSort(new BubbleStratege());
console.log(arr1, '应用冒泡策略')
//选择排序策略
let arr2 = context.setSort(new ChoosenStratege());
console.log(arr2, '应用选择策略')
//插入排序策略
let arr3 = context.setSort(new InsertStatege());
console.log(arr3, '应用插入策略')
}
main();
0|main | [ 33, 69, 59, 69, 25, 95, 66, 14, 27, 61, 43, 22, 38 ] '原始数组'
0|main | [ 14, 22, 25, 27, 33, 38, 43, 59, 61, 66, 69, 69, 95 ] '应用冒泡策略'
0|main | [ 14, 22, 25, 27, 33, 38, 43, 59, 61, 66, 69, 69, 95 ] '应用选择策略'
0|main | [ 14, 22, 25, 27, 33, 38, 43, 59, 61, 66, 69, 69, 95 ] '应用插入策略'
0|main | [ 64, 31, 69, 31 ] '原始数组'
0|main | [ 31, 31, 64, 69 ] '应用冒泡策略'
0|main | [ 31, 31, 64, 69 ] '应用选择策略'
0|main | [ 31, 31, 64, 69 ] '应用插入策略'
0|main | [ 59, 59, 85, 48, 0, 72, 95, 9, 22, 2, 27, 18, 17, 23, 59 ] '原始数组'
0|main | [ 0, 2, 9, 17, 18, 22, 23, 27, 48, 59, 59, 59, 72, 85, 95 ] '应用冒泡策略'
0|main | [ 0, 2, 9, 17, 18, 22, 23, 27, 48, 59, 59, 59, 72, 85, 95 ] '应用选择策略'
0|main | [ 0, 2, 9, 17, 18, 22, 23, 27, 48, 59, 59, 59, 72, 85, 95 ] '应用插入策略'
0|main | [ 24, 19, 56, 43, 28, 17, 24, 25, 91, 16, 18, 98, 27, 87, 95, 47, 49, 75 ] '原始数组'
0|main | [ 16, 17, 18, 19, 24, 24, 25, 27, 28, 43, 47, 49, 56, 75, 87, 91, 95, 98 ] '应用冒泡策略'
0|main | [ 16, 17, 18, 19, 24, 24, 25, 27, 28, 43, 47, 49, 56, 75, 87, 91, 95, 98 ] '应用选择策略'
0|main | [ 16, 17, 18, 19, 24, 24, 25, 27, 28, 43, 47, 49, 56, 75, 87, 91, 95, 98 ] '应用插入策略'
0|main | [ 34, 50, 27 ] '原始数组'
0|main | [ 27, 34, 50 ] '应用冒泡策略'
0|main | [ 27, 34, 50 ] '应用选择策略'
0|main | [ 27, 34, 50 ] '应用插入策略'
原文:https://www.cnblogs.com/pandapeter/p/11070156.html