策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以互相替换。
策略模式可用于缓动动画,表单验证等方面。
下面的代码是用于表单验证的策略模式。
/*******************策略类********************/
var strategies = {
isNonEmpty: 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;
}
},
};
/**********************Validator类*********************/
var Validator = function() {
this.cache = [];
};
Validator.prototype.add = function(dom, rules) {
var self = this;
for(var i = 0, rule; rule = rules[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);
})
})(rule);
}
};
Validator.prototype.start = function() {
for(var i = 0, validatorFunc; validatorFunc = this.cache[i++];) {
var errorMsg = validatorFunc();
if(errorMsg) {
return errorMsg;
}
}
};
/****************调用方法****************/
var validatorFunc = function () {
var validator = new Validator();
validator.add(
registerForm.userName,
[
{ strategy: 'isNonEmpty', errorMsg: '用户名不能为空' },
{ strategy: 'minLength:6', errorMsg: '用户名长度不能小于 10 位' },
],
);
validator.add(
registerForm.password,
[
{ strategy: 'minLength:6', errorMsg: '密码长度不能小于 6 位' },
],
);
validator.add(
registerForm.phoneNumber,
[
{ strategy: 'isMobile', errorMsg: '手机号码格式不正确' },
],
);
var errorMsg = validator.start();
return errorMsg;
}