← Back to Advanced Java Course | Chapter 6: Design Patterns | Lesson 9 of 14

Java का Strategy Pattern

Strategy pattern एक common interface के पीछे interchangeable algorithms का एक family define करता है, किसी object द्वारा उपयोग किए जाने वाले algorithm को उसका उपयोग करने वाले code से स्वतंत्र रूप से चुने या बदले जाने देते हुए।
Syntax
java
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?

java
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));
	}
}

Defining Interchangeable Algorithms

हर concrete strategy एक जैसा method signature implement करती है लेकिन बिल्कुल अलग internal logic के साथ, इसलिए strategy interface पर निर्भर code बिल्कुल एक जैसे काम करता है चाहे कोई भी concrete strategy असल में plug-in हो।

उदाहरण: Defining Interchangeable Algorithms

java
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));
	}
}

Switching Strategy at Runtime

Strategy pattern का उपयोग करने वाली एक class आमतौर पर अपनी current strategy का reference रखती है और उसे बदलने का तरीका उजागर करती है, configuration, user choice, या दूसरी conditions के आधार पर runtime पर algorithm बदलने देते हुए।

उदाहरण: Switching Strategy at Runtime

java
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));
	}
}

Strategy with Lambdas

Java 8 से, एक साधारण strategy को अक्सर सीधे Function या Comparator जैसे functional interface को implement करने वाले एक lambda expression के रूप में represent किया जा सकता है, एक पूरे interface और कई implementing classes की formalities से बचते हुए।

उदाहरण: Strategy with Lambdas

java
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));
	}
}

Strategy vs If-Else Chains

Strategy किसी type code के आधार पर behavior चुनने वाली एक लंबी if-else या switch chain का एक आम विकल्प है -- हर strategy को मौजूदा dispatch logic को कभी modify किए बिना एक नई class या lambda के रूप में जोड़ा जाता है।

उदाहरण: Strategy vs If-Else Chains

java
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));
	}
}
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 को एक null strategy के साथ छोड़ना, जो execute call होने पर NullPointerException फेंकता है; एक default सेट करें।
  2. context के अंदर लंबी if-else chain रखना, जो हर algorithm को उसकी अपनी strategy में ले जाने का मकसद ही हरा देता है।
  3. mutable state store करने वाले एक strategy object को कई threads के बीच share करना, जो race conditions का कारण बनता है।

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.