news 2026/3/1 2:25:28

常用设计模式:策略模式

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
常用设计模式:策略模式

策略模式(Strategy Pattern)是一种行为设计模式,它允许你定义一系列算法,并将每个算法封装起来,使它们可以相互替换。下面介绍策略模式在 TypeScript 中的实现。

策略模式基本概念

策略模式包含三个主要部分:

Context(上下文):维护一个策略对象的引用

Strategy(策略接口):定义所有支持的算法的公共接口

ConcreteStrategy(具体策略):实现策略接口的具体算法

image

基础实现

1. 定义策略接口

// 策略接口

interface PaymentStrategy {

pay(amount: number): void;

}

2. 实现具体策略类

// 信用卡支付策略

class CreditCardPayment implements PaymentStrategy {

private cardNumber: string;

private name: string;

constructor(cardNumber: string, name: string) {

this.cardNumber = cardNumber;

this.name = name;

}

pay(amount: number): void {

console.log(`使用信用卡支付 $${amount}`);

console.log(`卡号: ${this.cardNumber}, 持卡人: ${this.name}`);

}

}

// PayPal支付策略

class PayPalPayment implements PaymentStrategy {

private email: string;

constructor(email: string) {

this.email = email;

}

pay(amount: number): void {

console.log(`使用PayPal支付 $${amount}`);

console.log(`邮箱: ${this.email}`);

}

}

// 加密货币支付策略

class CryptoPayment implements PaymentStrategy {

private walletAddress: string;

constructor(walletAddress: string) {

this.walletAddress = walletAddress;

}

pay(amount: number): void {

console.log(`使用加密货币支付 $${amount}`);

console.log(`钱包地址: ${this.walletAddress}`);

}

}

3. 创建上下文类

// 支付上下文

class PaymentContext {

private strategy: PaymentStrategy;

constructor(strategy: PaymentStrategy) {

this.strategy = strategy;

}

// 设置支付策略

setStrategy(strategy: PaymentStrategy): void {

this.strategy = strategy;

}

// 执行支付

executePayment(amount: number): void {

this.strategy.pay(amount);

}

}

4. 使用示例

// 使用示例

const paymentContext = new PaymentContext(new CreditCardPayment("1234-5678-9012", "张三"));

// 使用信用卡支付

paymentContext.executePayment(100);

// 切换到PayPal支付

paymentContext.setStrategy(new PayPalPayment("zhang@example.com"));

paymentContext.executePayment(200);

// 切换到加密货币支付

paymentContext.setStrategy(new CryptoPayment("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"));

paymentContext.executePayment(300);

更复杂的示例:排序策略

// 排序策略接口

interface SortStrategy<T> {

sort(items: T[]): T[];

}

// 冒泡排序策略

class BubbleSort<T> implements SortStrategy<T> {

sort(items: T[]): T[] {

console.log("使用冒泡排序");

const arr = [...items];

for (let i = 0; i < arr.length; i++) {

for (let j = 0; j < arr.length - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];

}

}

}

return arr;

}

}

// 快速排序策略

class QuickSort<T> implements SortStrategy<T> {

sort(items: T[]): T[] {

console.log("使用快速排序");

if (items.length <= 1) return items;

const pivot = items[0];

const left = [];

const right = [];

for (let i = 1; i < items.length; i++) {

if (items[i] < pivot) {

left.push(items[i]);

} else {

right.push(items[i]);

}

}

return [...this.sort(left), pivot, ...this.sort(right)];

}

}

// 排序上下文

class Sorter<T> {

private strategy: SortStrategy<T>;

constructor(strategy: SortStrategy<T>) {

this.strategy = strategy;

}

setStrategy(strategy: SortStrategy<T>): void {

this.strategy = strategy;

}

sort(items: T[]): T[] {

return this.strategy.sort(items);

}

}

// 使用示例

const numbers = [64, 34, 25, 12, 22, 11, 90];

const sorter = new Sorter<number>(new BubbleSort<number>());

console.log("排序前:", numbers);

console.log("排序后:", sorter.sort(numbers));

// 切换排序策略

sorter.setStrategy(new QuickSort<number>());

console.log("使用快速排序:", sorter.sort(numbers));

使用函数式编程的实现

TypeScript 也支持函数式风格的策略模式:

// 策略类型定义

type DiscountStrategy = (amount: number) => number;

// 具体策略函数

const noDiscount: DiscountStrategy = (amount: number) => amount;

const percentageDiscount = (percentage: number): DiscountStrategy =>

(amount: number) => amount * (1 - percentage / 100);

const fixedDiscount = (discount: number): DiscountStrategy =>

(amount: number) => Math.max(0, amount - discount);

// 上下文

class ShoppingCart {

private items: number[] = [];

private discountStrategy: DiscountStrategy = noDiscount;

addItem(price: number): void {

this.items.push(price);

}

setDiscountStrategy(strategy: DiscountStrategy): void {

this.discountStrategy = strategy;

}

getTotal(): number {

const subtotal = this.items.reduce((sum, price) => sum + price, 0);

return this.discountStrategy(subtotal);

}

}

// 使用示例

const cart = new ShoppingCart();

cart.addItem(100);

cart.addItem(50);

cart.addItem(30);

console.log("原价:", cart.getTotal()); // 180

cart.setDiscountStrategy(percentageDiscount(10)); // 9折

console.log("9折后:", cart.getTotal()); // 162

cart.setDiscountStrategy(fixedDiscount(50)); // 减50

console.log("减50后:", cart.getTotal()); // 130

策略模式的优点

开闭原则:可以引入新策略而不修改现有代码

消除条件语句:避免大量的 if-else 或 switch-case 语句

算法复用:可以在不同的上下文中复用策略

测试友好:每个策略都可以独立测试

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/27 18:38:43

CogVideo革命性突破:2D视频秒变立体3D的智能转换技术

CogVideo革命性突破&#xff1a;2D视频秒变立体3D的智能转换技术 【免费下载链接】CogVideo text and image to video generation: CogVideoX (2024) and CogVideo (ICLR 2023) 项目地址: https://gitcode.com/GitHub_Trending/co/CogVideo 在AI视频生成领域&#xff0c…

作者头像 李华
网站建设 2026/2/27 19:28:30

DeepLabCut实战进阶:从姿态估计到强化学习环境的深度配置指南

想要将动物行为分析技术提升到工业级应用水平吗&#xff1f;DeepLabCut作为业界领先的无标记姿态估计框架&#xff0c;结合其强大的PyTorch后端和灵活的配置系统&#xff0c;能够为您的强化学习项目提供精准的行为数据支持。本文将从技术架构深度解析入手&#xff0c;通过对比两…

作者头像 李华
网站建设 2026/2/27 14:07:21

终极游戏DLC解锁指南:三步免费解锁付费内容

终极游戏DLC解锁指南&#xff1a;三步免费解锁付费内容 【免费下载链接】CreamApi 项目地址: https://gitcode.com/gh_mirrors/cr/CreamApi 想要免费解锁游戏中的付费DLC内容却不知从何入手&#xff1f;CreamInstaller游戏DLC解锁工具为您提供了简单直观的解决方案。这…

作者头像 李华
网站建设 2026/2/28 16:06:36

SeedVR2 2.5.10全面评测:8GB显存也能玩转的AI视觉增强神器

SeedVR2 2.5.10全面评测&#xff1a;8GB显存也能玩转的AI视觉增强神器 【免费下载链接】SeedVR2-3B 项目地址: https://ai.gitcode.com/hf_mirrors/ByteDance-Seed/SeedVR2-3B 作为字节跳动Seed实验室推出的新一代扩散式放大模型&#xff0c;SeedVR2 2.5.10版本在Comfy…

作者头像 李华
网站建设 2026/2/27 14:58:48

PCSX2模拟器性能优化终极指南:从卡顿到流畅的完整解决方案

PCSX2模拟器性能优化终极指南&#xff1a;从卡顿到流畅的完整解决方案 【免费下载链接】pcsx2 PCSX2 - The Playstation 2 Emulator 项目地址: https://gitcode.com/GitHub_Trending/pc/pcsx2 您是否在使用PCSX2模拟器时遇到画面卡顿、声音断续或游戏崩溃的问题&#xf…

作者头像 李华
网站建设 2026/2/25 20:43:33

告别卡顿:DBeaver性能优化终极指南

告别卡顿&#xff1a;DBeaver性能优化终极指南 【免费下载链接】dbeaver 项目地址: https://gitcode.com/gh_mirrors/dbe/dbeaver 你是否曾经在DBeaver中同时连接多个数据库时遭遇界面卡顿&#xff1f;执行复杂查询时软件响应缓慢&#xff1f;随着项目规模扩大&#xf…

作者头像 李华