策略模式通过将条件判断逻辑封装为独立策略类,使代码更清晰、易维护。1.定义策略接口,声明算法方法;2.创建具体策略类实现接口;3.环境类持有策略并执行;4.客户端通过环境类动态选择策略。适用于多条件分支且频繁变动的场景,如订单折扣、支付方式等。优点是符合开闭原则,缺点是类数量增加,客户端需了解所有策略。
策略模式,简单说,就是把一堆 if...else 或者 switch...case 语句,变成一个个独立的策略类,然后根据不同的情况选择不同的策略来执行。这样做的好处嘛,当然是让代码更清晰、更易于维护和扩展啦。
将条件判断替换为策略模式,核心在于定义一系列算法(策略),并将每个算法封装在独立的类中。 客户端可以根据运行时条件选择不同的策略,而无需修改现有代码。
当你发现代码中充斥着大量的条件判断,而且这些判断逻辑还经常变动,那么策略模式可能就是个不错的选择。举个例子,比如你有一个订单处理系统,根据不同的用户类型(普通用户、VIP 用户、超级 VIP 用户)有不同的折扣策略。如果直接用 if...else 来判断,代码会变得很臃肿,而且每增加一种用户类型,都要修改代码。这时候,用策略模式就能很好地解决这个问题。
定义策略接口: 声明所有具体策略需要实现的方法。比如,在订单折扣的例子中,可以定义一个 DiscountStrategy 接口,包含一个 calculateDiscount 方法。
// 策略接口 class DiscountStrategy { calculateDiscount(price) { throw new Error("Method 'calculateDiscount()' must be implemented."); } }
创建具体策略类: 实现策略接口,每个类代表一种具体的算法。例如,可以创建 NormalUserDiscount、VipUserDiscount、SuperVipUserDiscount 等类,分别实现不同的折扣计算逻辑。
// 具体策略类 - 普通用户折扣 class NormalUserDiscount extends DiscountStrategy { calculateDiscount(price) { return price * 0.9; // 九折 } } // 具体策略类 - VIP 用户折扣 class VipUserDiscount extends DiscountStrategy { calculateDiscount(price) { return price * 0.8; // 八折 } } // 具体策略类 - 超级 VIP 用户折扣 class SuperVipUserDiscount extends DiscountStrategy { calculateDiscount(price) { return price * 0.7; // 七折 } }
创建环境类(Context): 环境类负责维护一个策略对象,并提供一个方法来执行策略。客户端通过环境类来使用策略,而无需直接与具体策略类交互。
// 环境类 class Order { constructor(userType) { this.userType = userType; this.discountStrategy = this.createDiscountStrategy(userType); } createDiscountStrategy(userType) { switch (userType) { case "normal": return new NormalUserDiscount(); case "vip": return new VipUserDiscount(); case "superVip": return new SuperVipUserDiscount(); default: return new NormalUserDiscount(); // 默认策略 } } calculateFinalPrice(price) { return this.discountStrategy.calculateDiscount(price); } }
客户端使用: 客户端只需要创建环境类,并传入相应的参数,就可以使用不同的策略了。
// 客户端使用 const normalOrder = new Order("normal"); const vipOrder = new Order("vip"); const superVipOrder = new Order("superVip"); const price = 100; console.log(`普通用户最终价格:${normalOrder.calculateFinalPrice(price)}`); // 输出:普通用户最终价格:90 console.log(`VIP 用户最终价格:${vipOrder.calculateFinalPrice(price)}`); // 输出:VIP 用户最终价格:80 console.log(`超级 VIP 用户最终价格:${superVipOrder.calculateFinalPrice(price)}`); // 输出:超级 VIP 用户最终价格:70
上面的例子中,策略是在 Order 类的构造函数中确定的。如果需要在运行时动态切换策略,可以修改 Order 类,提供一个 setDiscountStrategy 方法。
class Order { constructor(userType) { this.userType = userType; this.discountStrategy = this.createDiscountStrategy(userType); } createDiscountStrategy(userType) { switch (userType) { case "normal": return new NormalUserDiscount(); case "vip": return new VipUserDiscount(); case "superVip": return new SuperVipUserDiscount(); default: return new NormalUserDiscount(); // 默认策略 } } setDiscountStrategy(discountStrategy) { this.discountStrategy = discountStrategy; } calculateFinalPrice(price) { return this.discountStrategy.calculateDiscount(price); } } // 客户端动态切换策略 const order = new Order("normal"); console.log(`初始价格:${order.calculateFinalPrice(100)}`); // 输出:初始价格:90 order.setDiscountStrategy(new VipUserDiscount()); console.log(`切换为 VIP 折扣后的价格:${order.calculateFinalPrice(100)}`); // 输出:切换为 VIP 折扣后的价格:80
策略模式的应用场景非常广泛,比如:
总而言之,策略模式是一种非常有用的设计模式,可以帮助我们编写更清晰、更易于维护和扩展的代码。当然,任何设计模式都不是银弹,需要根据具体的场景来选择是否使用。
以上就是js中如何用策略模式替换条件判断的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号