做服装有哪些好的网站做seo的网站

张小明 2026/1/17 8:43:29
做服装有哪些好的网站,做seo的网站,手机旅游网站建设,龙岗建设高端网站策略模式#xff08;Strategy Pattern#xff09;是一种行为设计模式#xff0c;它允许你定义一系列算法#xff0c;并将每个算法封装起来#xff0c;使它们可以相互替换。下面介绍策略模式在 TypeScript 中的实现。策略模式基本概念策略模式包含三个主要部分#xff1a;…策略模式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(zhangexample.com));paymentContext.executePayment(200);// 切换到加密货币支付paymentContext.setStrategy(new CryptoPayment(1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa));paymentContext.executePayment(300);更复杂的示例排序策略// 排序策略接口interface SortStrategyT {sort(items: T[]): T[];}// 冒泡排序策略class BubbleSortT implements SortStrategyT {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 QuickSortT implements SortStrategyT {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 SorterT {private strategy: SortStrategyT;constructor(strategy: SortStrategyT) {this.strategy strategy;}setStrategy(strategy: SortStrategyT): 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 Sorternumber(new BubbleSortnumber());console.log(排序前:, numbers);console.log(排序后:, sorter.sort(numbers));// 切换排序策略sorter.setStrategy(new QuickSortnumber());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()); // 180cart.setDiscountStrategy(percentageDiscount(10)); // 9折console.log(9折后:, cart.getTotal()); // 162cart.setDiscountStrategy(fixedDiscount(50)); // 减50console.log(减50后:, cart.getTotal()); // 130策略模式的优点开闭原则可以引入新策略而不修改现有代码消除条件语句避免大量的 if-else 或 switch-case 语句算法复用可以在不同的上下文中复用策略测试友好每个策略都可以独立测试
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

做视频网站的服务器小清新wordpress模板

Label Studio国际化配置实战指南:从零构建多语言数据标注平台 【免费下载链接】label-studio 项目地址: https://gitcode.com/gh_mirrors/lab/label-studio 在全球协作成为常态的今天,数据标注工具的多语言支持能力直接影响着跨国团队的使用体验…

张小明 2026/1/9 8:54:31 网站建设

网站制作 服务器 系统深圳网站建设方案优化

在日常办公中,我们经常会遇到这样的尴尬场景:一份长达几百页的PDF项目报告,太大发不出去;或者是需要将一份100页的资料,按“每20页一份”的规则,均匀分给5个小组去分别处理。如果手动一页页截图或者复制&am…

张小明 2026/1/2 14:54:05 网站建设

长沙在线建站模板做销售怎么和客户聊天

Perl编程:文件操作、哈希介绍及操作指南 在Perl编程中,文件处理和数据结构的运用是非常重要的部分。下面将详细介绍文件操作、哈希(Hash)的相关知识。 文件操作 在Perl里,文件句柄的使用对于文件的读写十分关键。以下两种方式在功能上是一样的,但通常为方便起见,我们…

张小明 2026/1/11 17:34:59 网站建设

外贸网站有什么向wordpress提交插件

DriverStoreExplorer:彻底解决Windows驱动存储空间占用问题的专业工具 【免费下载链接】DriverStoreExplorer Driver Store Explorer [RAPR] 项目地址: https://gitcode.com/gh_mirrors/dr/DriverStoreExplorer 在使用Windows系统的过程中,你是否…

张小明 2025/12/28 22:31:05 网站建设