Mastering the Strategy Pattern in Spring: Dynamic Message Processing Explained
This article introduces the Strategy design pattern, explains its core concept of interchangeable algorithms, and demonstrates a practical Spring-based implementation that routes various message types to specific services using an enum‑driven bean lookup, enabling flexible updates to Elasticsearch for merchant search and analytics.
Concept
The Strategy pattern (also called Policy Pattern) defines a family of algorithms, encapsulates each one, and makes them interchangeable so that the client using the algorithm is not affected by changes.
Practical Application
Business scenario: the system needs to listen to multiple message types, enrich each with different data, and update a single Elasticsearch index for merchant search and statistics. Different message types are processed by different strategies.
Implementation combines the Spring framework, a simple factory, and the Strategy pattern.
Strategy Interface
public interface GatherExecuteService {
/**
* Process the message body
* @param gatherDataVo
*/
boolean execute(GatherDataVo gatherDataVo);
}Concrete Strategy Implementations
// Price strategy implementation
@Service
public class PriceExecuteServiceImpl implements GatherExecuteService {
@Override
public boolean execute(GatherDataVo gatherDataVo) {
// ... implementation omitted
}
} // Product strategy implementation
@Service
public class ProductExecuteServiceImpl implements GatherExecuteService {
@Override
public boolean execute(GatherDataVo gatherDataVo) {
// ... implementation omitted
}
} // Stock strategy implementation
@Service
public class StockExecuteServiceImpl implements GatherExecuteService {
@Override
public boolean execute(GatherDataVo gatherDataVo) {
// ... implementation omitted
}
}Enum for Strategy Bean Lookup
@Getter
@AllArgsConstructor
public enum MessageTypeEnum {
PRODUCT(0, "productExecuteServiceImpl", "Product basic info message"),
PRICE(1, "priceExecuteServiceImpl", "Price message"),
STOCK(2, "stockExecuteServiceImpl", "Stock message");
private int type;
private String service;
private String description;
public static String getServiceName(int type) {
for (MessageTypeEnum enumType : MessageTypeEnum.values()) {
if (enumType.getType() == type) {
return enumType.getService();
}
}
return null;
}
}Using the Strategy
// Retrieve the appropriate strategy bean based on message type
String serviceName = MessageTypeEnum.getServiceName(gatherDataVo.getMessageType());
if (StringUtils.isNotBlank(serviceName)) {
GatherExecuteService gatherExecuteService = (GatherExecuteService) SpringContextUtil.getBean(
serviceName, GatherExecuteService.class);
}The Strategy pattern is a simple design pattern that is often combined with other patterns in real projects; this article shares a concise example.
JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
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.
