策略模式
问题
class ImageCompressor {
constructor() {}
compress(img, algorithm) {
let compressedImg = null
switch (algorithm) {
case 'jpg':
// jpg 的压缩算法
// compressedImg = ...
break
case 'png':
// png 的压缩算法
break
case 'gif':
// gif 的压缩算法
break
default:
break
}
return compressedImg
}
}
const imageCompressor = new ImageCompressor()
const pngImg = imageCompressor('exampleImageFile', 'png')
const jpgImg = imageCompressor('exampleImageFile', 'jpg')什么时候使用策略模式
一个简单例子
套用策略模式
策略模式的构成
Last updated