SOURCE

//实例一
const orderType = 1 // 1: 美妆,2:电器,3:家具
const orderWay = 2 // 1:h5,2:app,3:小程序

// 方案一
const strategy = () => { // 订单类型+环境类型策略
  const map = new Map([
    [{
      orderType: 1,
      orderWay: 1
    }, () => {
      console.log('美妆订单h5')
    }],
    [{
      orderType: 1,
      orderWay: 2
    }, () => {
      console.log('美妆订单app')
    }],
    [{
      orderType: 1,
      orderWay: 3
    }, () => {
      console.log('美妆订单小程序')
    }],
    [{
      orderType: 2,
      orderWay: 1
    }, () => {
      console.log('电器订单h5')
    }],
    [{
      orderType: 2,
      orderWay: 2
    }, () => {
      console.log('电器订单app')
    }],
    [{
      orderType: 2,
      orderWay: 3
    }, () => {
      console.log('电器订单小程序')
    }],
    [{
      orderType: 3,
      orderWay: 1
    }, () => {
      console.log('家具订单h5')
    }],
    [{
      orderType: 3,
      orderWay: 2
    }, () => {
      console.log('家具订单app')
    }],
    [{
      orderType: 3,
      orderWay: 3
    }, () => {
      console.log('家具订单小程序')
    }],
  ])
  return map
}

const run = (orderType, orderWay) => {
  let action = [...strategy()].filter(([key, value]) => (key.orderType === orderType && key.orderWay === orderWay))
  action.forEach(([key, value]) => value.call(this))
}

// run(orderType, orderWay)

// 方案二
const orderTypeH5Strategy = function() {
    return {
        1:  () => {
            console.log('美妆订单h5')
        },
        2: () => {
            console.log('电器订单h5')
        },
        3: () => {
            console.log('家具订单h5')
        }
    }
}
const orderTypeAppStrategy = function() {
    return {
        1:  () => {
            console.log('美妆订单app')
        },
        2: () => {
            console.log('电器订单app')
        },
        3: () => {
            console.log('家具订单app')
        }
    }
}
const orderTypeMinStrategy = function() {
    return {
        1:  () => {
            console.log('美妆订单min')
        },
        2: () => {
            console.log('电器订单min')
        },
        3: () => {
            console.log('家具订单min')
        }
    }
}
const wayStrategy = function() {
    return {
        1: orderTypeH5Strategy(),
        2: orderTypeH5Strategy(),
        3: orderTypeMinStrategy()
    }
}

const run1 = function(orderType, orderWay) {
    const orderTypeStrategy = wayStrategy()[orderWay]
    let orderTypeFn
    if (orderTypeStrategy) {
        orderTypeFn = orderTypeStrategy[orderType]
    }
    if (orderTypeFn) {
        orderTypeFn()
    }
}

run1(3, 3)
console 命令行工具 X clear

                    
>
console