Strategy Pattern की जानकारी
In this page:
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
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
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
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
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
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()));
- Context के अंदर एक type string पर
if/elseलगाना, जो अलग strategies का पूरा purpose defeat करता है। - एक strategy object pass करना जिसमें required method नहीं है, जैसे
pay। - 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: