Java का Strategy Pattern
In this page:
interface Strategy {
returnType execute(parameters);
}
class Context {
private Strategy strategy;
void setStrategy(Strategy s) { strategy = s; }
}
What is the Strategy Pattern?
Strategy pattern एक common interface के पीछे interchangeable algorithms का एक family define करता है, किसी object द्वारा उपयोग किए जाने वाले algorithm को उसका उपयोग करने वाले code से स्वतंत्र रूप से चुने या बदले जाने देते हुए।
उदाहरण: What is the Strategy Pattern?
public class Main {
interface Strategy { int execute(int a, int b); }
public static void main(String[] args) {
Strategy add = (a, b) -> a + b; // interchangeable behind a common interface
System.out.println(add.execute(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Defining Interchangeable Algorithms
हर concrete strategy एक जैसा method signature implement करती है लेकिन बिल्कुल अलग internal logic के साथ, इसलिए strategy interface पर निर्भर code बिल्कुल एक जैसे काम करता है चाहे कोई भी concrete strategy असल में plug-in हो।
उदाहरण: Defining Interchangeable Algorithms
public class Main {
interface Strategy { int execute(int a, int b); }
static class AddStrategy implements Strategy { public int execute(int a, int b) { return a + b; } }
static class MultiplyStrategy implements Strategy { public int execute(int a, int b) { return a * b; } }
public static void main(String[] args) {
Strategy s = new MultiplyStrategy(); // same signature, different internal logic
System.out.println(s.execute(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Switching Strategy at Runtime
Strategy pattern का उपयोग करने वाली एक class आमतौर पर अपनी current strategy का reference रखती है और उसे बदलने का तरीका उजागर करती है, configuration, user choice, या दूसरी conditions के आधार पर runtime पर algorithm बदलने देते हुए।
उदाहरण: Switching Strategy at Runtime
public class Main {
interface Strategy { int execute(int a, int b); }
static class Calculator {
Strategy strategy;
void setStrategy(Strategy strategy) { this.strategy = strategy; } // swap at runtime
int calculate(int a, int b) { return strategy.execute(a, b); }
}
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.setStrategy((a, b) -> a - b);
System.out.println(calc.calculate(10, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Strategy with Lambdas
Java 8 से, एक साधारण strategy को अक्सर सीधे Function या Comparator जैसे functional interface को implement करने वाले एक lambda expression के रूप में represent किया जा सकता है, एक पूरे interface और कई implementing classes की formalities से बचते हुए।
उदाहरण: Strategy with Lambdas
import java.util.function.BinaryOperator;
public class Main {
public static void main(String[] args) {
BinaryOperator<Integer> strategy = (a, b) -> a * b; // no separate class needed
System.out.println(strategy.apply(4, 5));
}
}
Login to try C/C++/Java/PHP code in the editor
Strategy vs If-Else Chains
Strategy किसी type code के आधार पर behavior चुनने वाली एक लंबी if-else या switch chain का एक आम विकल्प है -- हर strategy को मौजूदा dispatch logic को कभी modify किए बिना एक नई class या lambda के रूप में जोड़ा जाता है।
उदाहरण: Strategy vs If-Else Chains
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, java.util.function.BinaryOperator<Integer>> strategies = new HashMap<>();
strategies.put("add", (a, b) -> a + b);
strategies.put("sub", (a, b) -> a - b); // add more without touching existing dispatch code
System.out.println(strategies.get("add").apply(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
- context को एक
nullstrategy के साथ छोड़ना, जोexecutecall होने परNullPointerExceptionफेंकता है; एक default सेट करें। - context के अंदर लंबी
if-elsechain रखना, जो हर algorithm को उसकी अपनी strategy में ले जाने का मकसद ही हरा देता है। - mutable state store करने वाले एक strategy object को कई threads के बीच share करना, जो race conditions का कारण बनता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Java Design Patterns Introduction
- Java Singleton Pattern
- Java Factory Pattern
- Java Observer Pattern
- Java Builder Pattern
- Java MVC Architecture
- Java Adapter Pattern
- Java Decorator Pattern
- Java Strategy Pattern
- Java Command Pattern
- Java Facade Pattern
- Java Proxy Pattern
- Java Template Method Pattern
- Java Repository Pattern