Mastering the Strategy Pattern: Flexible Algorithms for Cleaner Code
This article explains the Strategy design pattern, its definition, structure, Java implementation with code examples, advantages, disadvantages, and typical scenarios where interchangeable algorithms improve flexibility and maintainability.
Definition of Strategy Pattern
Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable without affecting the client that uses them.
In everyday life a strategy is a plan; examples include choosing different transportation methods or various shopping promotions, each representing a distinct algorithm that can be swapped at runtime.
Structure of Strategy Pattern
The pattern involves three roles:
1. Context
The context holds a reference to a Strategy object and provides it to the client.
2. Abstract Strategy
An interface or abstract class that declares the operations all concrete strategies must implement.
3. Concrete Strategy
Implements the abstract strategy and encapsulates a specific algorithm or behavior.
Implementation in Java
1. Create the abstract strategy interface
package com.mikechen.java.design.strategy;
/**
* 抽象策略类
*
* @author mikechen
*/
public interface Strategy {
/**
* 策略方法
*/
public void strategyInterface();
}2. Create concrete strategies
ConcreteStrategyA
package com.mikechen.java.design.strategy;
/**
* 具体策略A
*
* @author mikechen
*/
public class ConcreteStrategyA implements Strategy {
@Override
public void strategyInterface() {
System.out.println("执行策略A");
}
}ConcreteStrategyB
package com.mikechen.java.design.strategy;
/**
* 具体策略B
*
* @author mikechen
*/
public class ConcreteStrategyB implements Strategy {
@Override
public void strategyInterface() {
System.out.println("执行策略B");
}
}3. Create the Context class
package com.mikechen.java.design.strategy;
/**
* 环境类
*
* @author mikechen
*/
public class Context {
// 持有一个具体策略的对象
private Strategy strategy;
/**
* 构造函数,传入一个具体策略对象
* @param strategy 具体策略对象
*/
public Context(Strategy strategy) {
this.strategy = strategy;
}
/**
* 策略方法
*/
public void contextInterface() {
strategy.strategyInterface();
}
}Advantages and Disadvantages
Advantages
Algorithms can be switched freely at runtime.
Avoids complex conditional statements, improving maintainability.
Good extensibility; adding a new strategy only requires implementing the interface.
Disadvantages
The number of strategy classes can grow quickly, with limited reuse.
All strategy classes must be exposed publicly.
Typical Use Cases
Multiple classes with slightly different algorithms or behaviors.
Scenarios requiring dynamic algorithm switching.
Situations where algorithm rules need to be hidden from the client.
Choosing transportation methods (bike, car, train, plane, etc.).
Shopping promotions such as discounts, bulk reductions, or rebates.
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.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
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.
