策略模式实现(验证)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34893429/article/details/78058045
// 定义策略
var strategy = {
    isNotEmpty: function(value, errorMsg){
        if(value === ''){
            return errorMsg;
        }
    },
    minLength: function(value, length, errorMsg){
        if(value.length < length){
            return errorMsg;
        }
    },
    mobileFormat: function(value, errorMsg){
        if(!/(^1[3|5|8][0-9]{9}$)/.test(value)){
            return errorMsg;
        }
    }
}
function Validator() {
    this.cache = [];
}
Validator.prototype.add = function(value, rules){
    for(var i = 0, rule; rule = rules[i++];){
        var self = this;
        (function(rule){
            self.cache.push(function(){
                var strategyRule = rule.strategy.split(':');
                var strategyName = strategyRule.shift();
                // 各位看官注意啦, 如果直接使用[value].concat(strategyRule.push(rule.errorMsg))会出问题
                // 什么问题呢? 
                // strategyRule.push(rule.errorMsg)这货会返回length, 我TM调试了半天!
                strategyRule.push(rule.errorMsg);
                var arr = [value].concat(strategyRule);

                return strategy[strategyName].apply(null,arr);
            })
        })(rule);
    }
}
Validator.prototype.check = function(){
    for(var i = 0, checkFn; checkFn = this.cache[i++];){
        var msg = checkFn();
        if(msg){
            return msg;
        }
    }
}
var validator = new Validator();
validator.add('12345', [
    {
        strategy: 'isNotEmpty',
        errorMsg: 'in not empty'
    },
    {
        strategy: 'minLength:10',
        errorMsg: 'length is less than 10'
    }
]);
var tip = validator.check(); // tip: length is less than 10
展开阅读全文

策略模式:接口和多个实现

05-25

<div>n <span style="font-size:16px;"><span><span style="font-size:20px;color:#FF0000;"><strong><span>课程亮点:</span></strong></span></span></span> n</div>n<div style="font-size:16px;">n <span style="color:#424242;"><span style="background-color:#FFFAA5;">课程培训详细的笔记以及实例代码,让学员开始掌握设计模式知识点</span></span> n</div>n<p style="font-size:16px;">n <span style="font-size:14px;"><span><span style="background-color:#FFFFFF;"><span style="color:#424242;"><br /></span></span></span></span> n</p>n<div>n <div>n <span style="font-size:16px;"><span><strong><span style="background-color:#FFFFFF;">课程内容:</span></strong></span></span>n </div>n <div>n <ol><li>n <span style="font-size:16px;color:#E53333;"><strong>工厂模式、桥接模式、组合模式、装饰器模式、外观模式、享元模式、原型模型、代理模式、单例模式、适配器模式</strong></span>n </li>n <li>n <span style="font-size:16px;color:#E53333;"><strong>策略模式、模板方法模式、观察者模式、迭代器模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式</strong></span>n </li>n </ol></div>n</div>n<div style="font-size:16px;">n <span style="color:#FF0000;"><strong><span style="font-size:18px;">课程特色:</span></strong></span> n</div>n<div>n <ol><li>n 笔记设计模式,用笔记串连所有知识点,让学员从一点一滴积累,学习过程无压力n </li>n <li>n 笔记标题采用关键字标识法,帮助学员更加容易记住知识点n </li>n <li>n 笔记以超链接形式让知识点关联起来,形式知识体系n </li>n <li>n 采用先概念后实例再应用方式,知识点深入浅出n </li>n <li>n <strong>提供授课内容笔记作为课后复习以及工作备查工具</strong> n </li>n </ol></div>n<p>n <span style="font-size:16px;"><span><span style="color:#00B050;"><strong><strong><span style="font-size:18px;">部分图表(电脑PC端查看):</span></strong></strong></span></span></span> n</p>n<p>n <span style="font-size:16px;"><span><span style="color:#00B050;"><strong><strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/201905251242348850.png" alt="" /><img src="https://img-bss.csdn.net/201905251242404542.png" alt="" /><img src="https://img-bss.csdn.net/201905251242469204.png" alt="" /><img src="https://img-bss.csdn.net/201905251242514951.png" alt="" /><img src="https://img-bss.csdn.net/201905251243028561.png" alt="" /><br /></span></strong></strong></span></span></span> n</p>

没有更多推荐了,返回首页