策略模式通过封装不同算法并允许运行时切换,提升代码灵活性与可维护性。以促销折扣计算为例,定义normal、gold、platinum、diamond等策略对象,结合https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3Calculator类实现动态切换,避免冗长条件判断。支持开闭原则,新增策略无需修改原有代码,适用于表单验证、排序、支付方式等场景。可通过StrategyContainer实现动态注册与执行,增强扩展性。

在前端开发中,面对多种计算或处理逻辑时,如何让代码更灵活、可维护?策略模式提供了一个优雅的解决方案。它把不同的算法封装成独立的策略类,允许在运行时根据需要动态切换,避免冗长的条件判断和重复代码。
策略模式定义了一系列算法,把它们各自封装起来,并且使它们可以互相替换。这些算法在结构上保持一致,外部可以透明地调用。
核心思想是:将“行为”或“算法”与“使用它的对象”解耦。比如表单验证、折扣计算、排序方式等场景都适合使用策略模式。
假设我们要实现一个促销价格计算器,不同会员等级享受不同折扣:
立即学习“Java免费学习笔记(深入)”;
传统做法可能会用一连串 if-else 或 switch 判断,但随着规则增多,代码会变得难以维护。使用策略模式可以清晰分离每种计算逻辑。
代码实现如下:
// 定义策略对象
const discountStrategies = {
normal: (https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3) => https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3,
gold: (https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3) => https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3 * 0.9,
platinum: (https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3) => https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3 * 0.8,
diamond: (https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3) => https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3 * 0.7
};
<p>// 上下文类:负责调用策略
class https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3Calculator {
constructor(strategy = 'normal') {
this.setStrategy(strategy);
}</p><p>setStrategy(strategy) {
if (!discountStrategies[strategy]) {
throw new Error(<code>不支持的策略: ${strategy}</code>);
}
this.strategy = strategy;
}</p><p>calculate(https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3) {
return discountStrategies<a href="https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3">this.strategy</a>;
}
}</p><p>// 使用示例
const calculator = new https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3Calculator('gold');
console.log(calculator.calculate(100)); // 输出 90</p><p>calculator.setStrategy('diamond');
console.log(calculator.calculate(100)); // 输出 70</p>策略模式带来的好处很明显:
常见适用场景包括:
为了进一步提高灵活性,可以将策略注册机制抽离出来,支持动态注册新策略:
class StrategyContainer {
constructor() {
this.strategies = {};
}
<p>add(name, fn) {
this.strategies[name] = fn;
}</p><p>execute(name, https://www.php.cn/link/1f1bf7748e7e06682e348b22a722266a) {
if (!this.strategies[name]) {
throw new Error(<code>策略不存在: ${name}</code>);
}
return this.strategies<a href="https://www.php.cn/link/1f1bf7748e7e06682e348b22a722266a">name</a>;
}
}</p><p>// 动态添加策略
const discounts = new StrategyContainer();
discounts.add('student', https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3 => https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3 <em> 0.95);
discounts.add('vip', https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3 => https://www.php.cn/link/faa0d2f81e8c5bb3f42df059d58ae0c3 </em> 0.6);</p><p>console.log(discounts.execute('student', 100)); // 95</p>这种方式更适合插件化或配置化的系统设计。
基本上就这些。策略模式不复杂,但在需要多算法共存并动态切换的场景下非常实用。关键是把每个算法当作“一等公民”来对待,统一接口,自由替换。
以上就是JavaScript策略模式_多算法动态切换的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号