zoukankan      html  css  js  c++  java
  • 二、js前端策略者模式

    1.定义一系列的算法

    2.将这些算法封装起来

    3.可以让它们相互替换

    const EmployeeA = function(){}
    EmployeeA.prototype.calculate = function(salary){
    return salary * 4;
    }
    const EmployeeB = function(){}
    EmployeeB.prototype.calculate = function(salary){
    return salary * 3;
    }
    const EmployeeC = function(){}
    EmployeeC.prototype.calculate = function(salary){
    return salary * 2;
    }
    const Bounds = function(){
    this.salary = null;
    this.levelObj = null;
    }
    Bounds.prototype.setSalary = function(salary){
    this.salary = salary
    }
    Bounds.prototype.setLevelObj = function(levelObj){
    this.levelObj = levelObj
    }
    Bounds.prototype.getBounds = function(){
    return this.levelObj.calculate(this.salary)
    }
    const bounds = new Bounds();
    bounds.setSalary(10000)
    bounds.setLevelObj(new EmployeeA())
    console.info(bounds.getBounds());

     注1.柯里化处理

         function currying(fn){

                   let arr = [];

                  return function cd(){

                        if(arguments.length == 0) fn.apply(this,arr);

                       Array.prototype.push.apply(arr,[].slice.call(arguments));

                        return cd

                 }

         }

     注2.https://blog.csdn.net/song_mou_xia/article/details/80763833设计模式

  • 相关阅读:
    JS(原生语法)_实现酷酷的动态简历
    Linux外在设备的使用
    查看系统内存信息
    查看CPU信息
    查看系统PCI设备
    配置网络
    Linux分区
    Observer
    Singleton
    Open closed principle
  • 原文地址:https://www.cnblogs.com/wangc04/p/12983900.html
Copyright © 2011-2022 走看看