//阅读理解javascript中策略模式练习
//文章链接如下http://www.360doc.com/content/15/0819/11/15077656_493376894.shtml
//奖金demo

var calcuateSalary = function(salary, level){
	if(level === 'A'){
		return salary * 4;
	}else if(level === 'B'){
		return salary * 3;
	}else if(level === 'C'){
		return salary * 2;
	}
};

//存在缺点：存在很多if else判断   函数缺乏弹性，如果再来个D,需要在函数内添加判断， 算法不能复用 

//面向对象的方式来实现奖金的计算
var stragtegy = {
    'A' : function(salary) {
        return salary * 4;
    },
    'B' : function(salary) {
        return salary * 3;
    },
    'C' : function(salary) {
        return salary * 2;
    }
};

var bonus = function(salary) {
    this.salary = null;
    this.levelObj = null;
};

bonus.prototype.setSalary = function(salary) {
    this.salary = salary;
};

bonus.prototype.setLevel= function(levelObj){
    this.levelObj = levelObj;
};
bonus.prototype.getBonus = function(){
    return this.levelObj(this.salary);
};


var a = new bonus();
a.setSalary(1000);
a.setLevel(stragtegy['A']);
console.log(a.getBonus());

var b = new bonus();
b.setSalary(2000);
b.setLevel(stragtegy['B']);
console.log(b.getBonus());

var c = new bonus();
c.setSalary(1000);
c.setLevel(stragtegy['C']);
console.log(c.getBonus());


@@ -1175,20 +1175,1565 @@
 log(c.getBonus());%0A%0A
+//%E5%A5%96%E9%87%91%E7%B1%BB%E6%99%AE%E9%80%9A%E5%AE%9E%E7%8E%B0%0Avar calSalary = function(level,salary)%7B%0A    return stragtegy%5Blevel%5D(salary);%0A%7D;%0A%0A%0A//%E8%A1%A8%E5%8D%95%E7%AD%96%E7%95%A5%E7%B1%BB%E5%AE%9E%E7%8E%B0%0Avar formStrategy = %7B%0A    isNotEmpty:function(value,errorMsg)%7B%0A        if(value === '')%7B%0A            return errorMsg;%0A        %7D%0A    %7D,%0A    minLength : function(value, length, errorMsg)%7B%0A        if(value %3C length)%7B%0A            return errorMsg;%0A        %7D%0A    %7D,%0A    mobileFormate : function(value, errorMsg)%7B%0A        if(!/%5B1-9%5D/.test(value))%7B%0A            return errorMsg;%0A        %7D%0A    %7D%0A%7D;%0A%0Avar Validator = function()%7B%0A    this.cache=%5B%5D; //%E5%82%A8%E5%AD%98%E6%A0%A1%E9%AA%8C%E8%A7%84%E5%88%99%0A%7D;%0A%0AValidator.prototype = %7B%0A    add : function(dom,rule,errorMsg)%7B%0A        var item = rule.split(':');%0A        this.cache.push(function()%7B%0A            var strage = item.shift();%0A            item.unshift(dom.value);%0A            item.push(errorMsg);%0A            return formStrategy%5Bstrage%5D.apply(dom,item);%0A        %7D);%0A    %7D,%0A    start:function()%7B%0A        var len = this.cache.length;%0A        for(var i=0; i %3C len; i++)%7B%0A            var msg = this.cache%5Bi%5D();%0A            if(msg)%7B%0A                return msg;%0A            %7D%0A        %7D%0A    %7D%0A%7D;%0A%0Avar validatorFunc = function()%7B%0A    var validator = new Validator();%0A    validator.add(form.userName, 'isNotEmpty', '%E7%94%A8%E6%88%B7%E5%90%8D%E4%B8%8D%E8%83%BD%E4%B8%BA%E7%A9%BA');%0A    validator.add(form.pwd, 'minLength:6', '%E5%AF%86%E7%A0%81%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E4%B8%BA6');%0A    validator.add(form.mobilePhone,'mobileFormate', '%E6%89%8B%E6%9C%BA%E5%8F%B7%E4%B8%8D%E7%AC%A6%E5%90%88%E8%A7%84%E5%88%99');%0A    return validator.start();%0A%7D%0A%0Avar form = document.getElementById('form');%0Aform.onSubmit = function()%7B%0A    var msg = validatorFunc();%0A    if(msg)%7B%0A        alert(msg);%0A        return;%0A    %7D%0A%7D;%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A

add strategy pattern