Understanding the Strategy Design Pattern in Java
The Strategy design pattern in Java encapsulates interchangeable algorithms behind a common interface, using concrete strategy classes and a context holder to delegate behavior at runtime, which enhances flexibility, adheres to the Open/Closed principle, reduces conditional complexity, though it may increase class count and require client awareness of available strategies.
Strategy pattern is a widely used behavioral design pattern that encapsulates algorithms and allows them to be swapped at runtime.
In software development, reusability and low coupling are essential; design patterns provide proven solutions to achieve maintainable and extensible code.
The core concepts of the Strategy pattern are:
Abstract Strategy : a common interface that defines the algorithm contract.
Concrete Strategy : classes that implement the abstract strategy with specific algorithms.
Context : a class that holds a reference to an abstract strategy and delegates calls to it.
Structure overview:
Abstract Strategy (Strategy) → Concrete Strategies (ConcreteStrategyA/B/C) → Context (Context_TaoPlatform) → Client.
Usage example in Java:
public interface Strategy {
void show();
}
public class ConcreteStrategyA implements Strategy {
@Override
public void show() {
System.out.println("618大促");
}
}
public class ConcreteStrategyB implements Strategy {
@Override
public void show() {
System.out.println("99大促");
}
}
public class ConcreteStrategyC implements Strategy {
@Override
public void show() {
System.out.println("双11大促");
}
}
public class Context_TaoPlatform {
private Strategy myStrategy;
public Context_TaoPlatform(Strategy strategyType) {
this.myStrategy = strategyType;
}
public void taoPlatformShow(String time) {
System.out.println(time + "的促销策略是:");
myStrategy.show();
}
}
public class StrategyPattern {
public static void main(String[] args) {
Context_TaoPlatform context;
String time1 = "9月";
Strategy strategyB = new ConcreteStrategyB();
context = new Context_TaoPlatform(strategyB);
context.taoPlatformShow(time1);
// similar code for other strategies …
}
}The same principle appears in the JDK. In Arrays.sort, the Comparator interface acts as the abstract strategy, while the concrete comparator implementations provide specific ordering logic.
public static <T> void sort(T[] a, Comparator<? super T> c) {
if (c == null) {
sort(a); // default ascending order
} else {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, 0, a.length, c, null, 0, 0);
}
}Advantages
Concrete strategies can be switched freely without modifying client code.
Supports the Open/Closed principle – new algorithms are added by creating new strategy classes.
Eliminates complex conditional statements, improving readability.
Disadvantages
The client must be aware of all concrete strategy classes to choose the appropriate one.
Introduces many small classes, potentially increasing system complexity.
Typical applicability
When a system must select one of several algorithms at runtime.
When multiple behaviors are encapsulated and should be interchangeable.
When algorithm details need to be hidden from the client for security or encapsulation reasons.
In summary, the Strategy pattern simplifies algorithm management, promotes flexibility, and helps keep codebases clean and maintainable.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
