Multi-If function encapsulation strategy

        In our work, we often encounter multiple if interpretation functions, which is a very normal thing, as follows:

let order = function (orderType, isPay, count) {
  if (orderType === 1) {
    // 充值 500
    if (isPay === true) {
      // 充值成功
      console.log('中奖100元')
    } else {
      if (count > 0) {
        console.log('抽到10元优惠卷')
      } else {
        console.log('请再接再励!')
      }
    }
  } else if (orderType === 2) {
    // 充值 200
    if (isPay === true) {
      // 充值成功
      console.log('中奖20元')
    } else {
      if (count > 0) {
        console.log('抽到10元优惠卷')
      } else {
        console.log('请再接再励!')
      }
    }
  } else if (orderType === 3) {
    // 不充钱
    if (count > 0) {
      console.log('抽到10元优惠卷')
    } else {
      console.log('请再接再励!')
    }
  }
}

                But in many cases, the if judgment in it will include many methods and data processing, so that a function often reaches more than 200 lines, so is there any way to optimize it? Let's take a look today.

1. Defined strategy

        To put it in a nutshell, each situation is the first method, and the parameters can be passed in for judgment when needed.

const strategy = {
  order500: function (isPay, count) {
    if (isPay === true) {
      // 充值成功
      console.log('中奖100元')
    } else {
      this.orderNoraml(isPay, count)
    }
  },
  order200: function (isPay, count) {
    // 充值 200
    if (isPay === true) {
      // 充值成功
      console.log('中奖20元')
    } else {
      this.orderNoraml(isPay, count)
    }
  },
  orderNoraml: function (isPay, count) {
    // 不充钱
    if (count > 0) {
      console.log('抽到10元优惠卷')
    } else {
      console.log('请再接再励!')
    }
  },
}

        Then pass in the value for function judgment when using it, and then return the required parameters.

function orderFunc(orderType, isPay, count) {
  let orderTypeObj = {
    1: 'order500',
    2: 'order200',
    3: 'orderNoraml'
  };
   return strategy[orderTypeObj[orderType]](isPay, count);
}

orderFunc(1, true, 0)
orderFunc(1, false, 100)
orderFunc(2, true, 0)

2. Chain of Responsibility Model

        Its definition is to deal with the situation that you can handle at present, if you can't handle it, then go to the next function until you can handle it.

//原则:只处理单一模式,处理不了之后往后传
function order500(orderType, isPay, count) {
  if (orderType === 1 && isPay === true) {
      // 充值成功
      console.log('中奖100元')
    } else {
      order200(orderType, isPay, count)
    }
}

function order200(orderType, isPay, count) {
 // 充值 200
 if (orderType === 2 && isPay === true) {
      // 充值成功
      console.log('中奖20元')
    } else {
      orderNoraml(orderType, isPay, count)
    }
}

function orderNoraml(orderType, isPay, count) {
  // 不充钱
  if (count > 0) {
      console.log('抽到10元优惠卷')
    } else {
      console.log('请再接再励!')
    }
}

        Calling a method only needs to execute a function

order500(1, true, 100)
order500(1, true, 100)
order500(2, true, 0)

3. Functional mode

        In fact, I feel that the second method should be a simplified version, so that each method inherits his next method, and then let them call each other to form a cycle.

        The definition of the method is mainly reflected in the next method.

function order500(orderType, isPay, count) {
  if (orderType === 1 && isPay === true) {
      // 充值成功
      console.log('中奖100元')
    } else {
      return 'next'
    }
}

function order200(orderType, isPay, count) {
 // 充值 200
 if (orderType === 2 && isPay === true) {
      // 充值成功
      console.log('中奖20元')
    } else {
      return 'next'
    }
}

function orderNoraml(orderType, isPay, count) {
  // 不充钱
  if (count > 0) {
      console.log('抽到10元优惠卷')
    } else {
      console.log('请再接再励!')
    }
}

        Define a class class for inheritance

class chain {
  constructor(fn) {
    this.fn = fn
    this.next = null
  }

  // 设置下一链条
  setNext(nextChain) {
    this.next = nextChain
  }

  // Run 起来
  passRequest() {
    console.log("this===>>",this)
    let res = this.fn.apply(this,arguments)
    if(res === 'next') {
      return this.next && this.next.passRequest.apply(this.next,arguments)
    }
    return res
  }
}

Then implement it and call each other.

let order500Chain = new chain(order500)
let order200Chain = new chain(order200)
let orderNoramlChain = new chain(orderNoraml)

order500Chain.setNext(order200Chain)
order200Chain.setNext(orderNoramlChain)

order500Chain.passRequest(1, false, 100)

        Isn’t this method very ingenious? In the process of our work and study, you can also try to make them into a functional form, let’s work hard together.

Guess you like

Origin blog.csdn.net/qq_43185384/article/details/129126031