← Back to TypeScript Course | Chapter 16: Advanced Patterns | Lesson 5 of 7

Strategy Pattern की जानकारी

Strategy Pattern interchangeable algorithms को एक common interface के पीछे रखता है। एक context अपनी core logic बदले बिना strategies switch कर सकता है।
Syntax
typescript
interface Strategy {
  execute(data: type): returnType;
}
class Context {
  constructor(private strategy: Strategy) {}
  run(data: type) { return this.strategy.execute(data); }
}

Basic Strategy

एक strategy एक common interface के पीछे एक algorithm represent करती है, जबकि एक context object इसे दी गई चाहे जो भी strategy हो उसका एक reference रखता है और असली काम इसे delegate करता है।

यानी context को behavior चुनने के लिए कभी if/else chain की ज़रूरत नहीं — यह सिर्फ उस strategy को call करता है जो इसे दी गई।

उदाहरण: Basic Strategy

typescript
interface PaymentStrategy {
  pay(amount: number): string;
}
class CreditCard implements PaymentStrategy {
  pay(amount: number) { return `Paid ${amount} by credit card`; }
}
class Context {
  constructor(private strategy: PaymentStrategy) {}
  checkout(amount: number) { return this.strategy.pay(amount); }
}
console.log(new Context(new CreditCard()).checkout(100));

Strategies Switch करना

एक context एक setter expose कर सकता है ताकि इसकी strategy program चलते हुए swap हो सके, वही object को user input, configuration, या runtime conditions के response में फिर से बनाए बिना behavior बदलने देते हुए।

उदाहरण: Switching Strategies

typescript
interface PaymentStrategy {
  pay(amount: number): string;
}
class PayPal implements PaymentStrategy {
  pay(amount: number) { return `Paid ${amount} via PayPal`; }
}
class Context {
  private strategy: PaymentStrategy;
  constructor(strategy: PaymentStrategy) { this.strategy = strategy; }
  setStrategy(strategy: PaymentStrategy) { this.strategy = strategy; }
  checkout(amount: number) { return this.strategy.pay(amount); }
}
const ctx = new Context(new PayPal());
console.log(ctx.checkout(50));

Function Strategies

Simple algorithms के लिए, एक typed function इसके लिए अलग class बनाए बिना एक strategy के रूप में काम कर सकता है — TypeScript की structural typing का मतलब है expected signature से match करने वाली कोई भी function अपने आप compatible है।

उदाहरण: Function Strategies

typescript
type SortStrategy = (a: number, b: number) => number;
const ascending: SortStrategy = (a, b) => a - b;
const descending: SortStrategy = (a, b) => b - a;
function sortWith(nums: number[], strategy: SortStrategy) {
  return [...nums].sort(strategy);
}
console.log(sortWith([3, 1, 2], ascending));

Strategy के फायदे

Strategy बड़े conditional blocks कम करती है और हर algorithm को isolate करती है ताकि उसे independently test, replace, या extend किया जा सके, जो सबसे ज़्यादा तब मायने रखता है जब एक codebase में same operation के कई interchangeable variants हों।

उदाहरण: Benefits of Strategy

typescript
interface Discount {
  apply(price: number): number;
}
class NoDiscount implements Discount {
  apply(price: number) { return price; }
}
class TenPercentOff implements Discount {
  apply(price: number) { return price * 0.9; }
}
function checkout(price: number, discount: Discount) {
  return discount.apply(price);
}
console.log(checkout(100, new TenPercentOff()));

Strategy कब उपयोग करें

Strategy उपयोग करें जब किसी operation के सच में कई interchangeable approaches हों जो runtime पर बदल सकते हैं या समय के साथ बढ़ सकते हैं। इसे avoid करें जब एक simple algorithm काफी हो — extra interface एक indirection जोड़ता है जिसकी आपको अभी ज़रूरत नहीं।

उदाहरण: When to Use Strategy

typescript
interface Formatter {
  format(value: number): string;
}
class PlainFormatter implements Formatter {
  format(value: number) { return String(value); }
}
function display(value: number, formatter: Formatter) {
  return formatter.format(value);
}
console.log(display(42, new PlainFormatter()));
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Context के अंदर एक type string पर if/else लगाना, जो अलग strategies का पूरा purpose defeat करता है।
  2. एक strategy object pass करना जिसमें required method नहीं है, जैसे pay।
  3. Strategy method call करना भूल जाना (this.strategy.pay(amount)), और सिर्फ strategy return कर देना।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.