nicenote/docs/fea/pattern/strategy.md
2021-08-25 20:10:25 +08:00

792 B

nav group
title path
前端 /fea
title order
💊 设计模式 7

策略模式

class CalculateMoney {
    constructor(type, money) {
        this.money = money
        return this.switchStrategy(type, money)
    }

    /**
     * 
     * @param {string} type 类型
     * @param {number} money 金额 
     */
    switchStrategy(type, money) {
        let tp = type.toLocaleLowerCase()
        if (tp === 'a') {
            return this.money = money * 100
        } else if (tp === 'b') {
            return this.money = money * 200
        } else if (tp === 'c') {
            return this.money = money * 300
        } else {
            return this.money = money * 400
        }
    }
}


let calculateMoney = new CalculateMoney('', 10000)
// => 4000000