# javascript的策略模式（一）

2016/05/17 17:54

``````var performanceS= function(salary){
return salary*4;
}
var performanceA= function(salary){
return salary*3;
}
var performanceB= function(salary){
return salary*2;
}
varcalculateBonus = function(performanceLevel,salary){
if(performanceLevel==”S”){
return performanceS(salary);
}
if(performanceLevel==”A”){
return performanceA(salary);
}
if(performanceLevel==”B”){
return performanceB(salary);
}
}
calculateBonus(‘A’,10000);``````

l   if-else分支多，这些分支要覆盖所有的逻辑

l   calculateBonus函数缺乏弹性，如果增加了一种新的绩效等级C，或是把绩效S的奖金系数改为5，那么我们必须深入calculateBonus函数的内部实现，这违反开放—封闭原则

l   算法的复用性差，如果在程序的其他地方需要重用这些计算奖金的算法呢？我们只有复制和粘贴。

``````var strategies = {
“S”:function(salary){
return salary*4;
},
“A”:function(salary){
return salary*4;
},
“B”:function(salary){
return salary*4;
}
};``````
``````var calculateBonus =function(level,salary){
return strategies[level](salary);
}
console.log(calculateBonus(‘S’,20000)); //输出80000
console.log(calculateBonus(‘S’,10000)); //输出30000``````

策略模式的目的就是将算法的使用与算法的实现分离开来。

``````<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8">

<body>
<form action='' id='registerForm' method='post'>
请输入密码：<input type='text' name = 'password' /> <br>
请输入手机号码：<input type='text' name = 'phoneNumber' />  <br>
<input type='submit' value = '提交' />
</form>
<script src="index.js"></script>
</body>
</html>``````

index.js

``````//javascript

var strategies = {
isNoEmpty: function(value, errorMsg){
if(value == ''){
return errorMsg;
}
},
minLength: function(value, length, errorMsg){
if(value.length < length){
return errorMsg;
}
},
isMobile: function(value, errorMsg){
if(!/(^1[3|5|8][0-9]{9}\$)/.test(value)){
return errorMsg;
}
}

}

var Validator = function(){
this._cache = [];
}

var self = this;
for(var i=0; i<rules.length; i++){
(function(rule){
var strategyAry = rule.strategy.split(':');
var errorMsg = rule.errorMsg;
self._cache.push(function(){
var strategy = strategyAry.shift();
strategyAry.unshift(dom.value);
strategyAry.push(errorMsg);
return strategies[strategy].apply(dom, strategyAry);
});
})(rules[i])

}

};

Validator.prototype.start = function(){
for(var i=0, validatorFunc;validatorFunc = this._cache[i++];){
var errorMsg = validatorFunc();
if(errorMsg){
return errorMsg;
}

}

}

var registerForm = document.getElementById('registerForm');
var validataFunc = function(){
var validator = new Validator();
console.log(validator);
{
strategy:'isNoEmpty',
errorMsg:'用户名不能为空'
},
{
strategy:'minLength:10',
errorMsg:'用户名长度不能小于10'
}

]);
{
strategy:'minLength:6',
errorMsg:'密码长度不能小于6'
}

]);
{
strategy:'isMobile',
errorMsg:'手机号码格式不正确'
}

]);

var errorMsg = validator.start();
return errorMsg;

}

registerForm.onsubmit = function(){
var errorMsg = validataFunc();
if(errorMsg){
return false;
}

}
``````

2
0 收藏

0 评论
0 收藏
2