Preface 策略模式指的是定义一系列的算法,把它们一个个封装起来。将不变的部分和变化的部分隔开是每个设计模式的主题,策略模式也不例外,策略模式的目的就是将算法的使用与算法的实现分离开来。
一个基于策略模式的程序至少由两部分组成。第一个部分是一组策略类,策略类封装了具体的算法,并负责具体的计算过程。第二个部分是环境类Context,Context接受客户的请求,随后把请求委托给某一个策略类。要做到这点,说明Context中要维持对某个策略对象的引用
策略模式实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var strategies = { "S" : function (salary ) { return salary * 4 ; }, "A" : function (salary ) { return salary * 3 ; }, "B" : function (salary ) { return salary * 2 ; } } var calculateBonus = function (level, salary ) { return strategies[level](salary); }; console .log(calculateBonus('S' , 2000 ));console .log(calculateBonus('A' , 1000 ));
使用策略模式实现缓动动画 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 var tween = { linear: function (t, b, c, d ) { return c*t/d + b; }, easeIn: function (t, b, c, d ) { return c * (t /= d) * t + b; }, strongEaseIn: function (t, b, c, d ) { return c * (t /= d) * t * t * t * t + b; }, strongEaseOut: function (t, b, c, d ) { return c * ((t = t / d - 1 ) * t * t * t * t + 1 ) + b; }, sineaseIn: function (t, b, c, d ) { return c * (t /= d) * t * t + b; }, sineaseOut: function (t, b, c, d ) { return c * ((t = t / d - 1 ) * t * t + 1 ) + b; }, } var Animate = function (dom ) { this .dom = dom; this .startTime = 0 ; this .startPos = 0 ; this .endPos = 0 ; this .propertyName = null ; this .easing = null ; this .duration = null ; } Animate.prototype.start = function (propertyName, endPos, duration, easing ) { this .startTime = +new Date ; this .startPos = this .dom.getBoundingClientRect()[propertyName]; this .propertyName = propertyName; this .endPos = endPos; this .duration = duration; this .easing = tween[easing]; var self = this ; var timeId = setInterval(function ( ) { if (self.step() === false ) { clearInterval(timeId); } }, 19 ) } Animate.prototype.step = function ( ) { var t = +new Date ; if (t >= this .startTime + this .duration) { this .update(this .endPos); return false ; } var pos = this .easing(t - this .startTime, this .startPos, this .endPos - this .startPos, this .duration); this .update(pos); }; Animate.prototype.update = function (pos ) { this .dom.style[this .propertyName] = pos + 'px' ; } var div = document .getElementById('div' );var animate = new Animate(div);animate.start('top' , 1500 , 500 , 'linear' );
参考 《JavaScript设计模式与开发实践》