Simplify Multi‑Channel Order Processing with Template, Factory & Strategy Patterns
This article demonstrates how to use the Template Method, Factory Method, and Strategy patterns in a Java Spring e‑commerce fulfillment system to clearly separate channel‑specific order handling, improve code maintainability, and enable rapid addition of new order types.
In e‑commerce, fulfillment and payment systems often need to handle multiple order channels such as recharge orders, regular orders, and split‑item orders, each requiring distinct processing logic. The article uses a case study of implementing order receipt in a fulfillment system to show how design patterns can make the business clearer and the code easier to maintain.
1. Template Method defines common processing logic
After receiving an order message, the system first checks whether the order needs to be processed (e.g., third‑party orders may be excluded). Order validation is a fixed step, so it is encapsulated in a template method. Pseudocode and a concrete Java abstract class ReceiveOrderTemplate are provided, with a private method checkNeedReceiveOrder that cannot be overridden by subclasses.
<code>public abstract class ReceiveOrderTemplate implements OrderStrategy {
public void receiveOrder(Integer orderType) {
// Detect if order needs to be received
if (!checkNeedReceiveOrder(orderType)) {
return;
}
// Execute receipt logic
handlerOrder(orderType);
}
private boolean checkNeedReceiveOrder(Integer orderType) {
return true;
}
}
</code>2. Factory Method selects the execution strategy
The system supports several strategies based on orderType . A factory method, integrated with Spring, returns the appropriate strategy bean.
<code>@Component
public class ReceiveOrderFactory implements ApplicationContextAware {
private ApplicationContext applicationContext;
public ReceiveOrderTemplate getOrderStrategy(Integer orderType) {
switch (orderType) {
case 10: return applicationContext.getBean(RechargeOrderStrategy.class);
case 20: return applicationContext.getBean(NormalOrderStrategy.class);
case 30: return applicationContext.getBean(SplitOrderStrategy.class);
default: throw new RuntimeException("No matching strategy");
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
</code>3. Strategy implementations contain the specific business logic
Each concrete strategy extends ReceiveOrderTemplate and overrides handlerOrder to perform channel‑specific actions.
<code>public interface OrderStrategy {
void handlerOrder(Integer orderType);
}
// Recharge order strategy
@Component
public class RechargeOrderStrategy extends ReceiveOrderTemplate {
@Override
public void handlerOrder(Integer orderType) {
System.out.println("Recharge order received");
}
}
// Normal order strategy
@Component
public class NormalOrderStrategy extends ReceiveOrderTemplate {
@Override
public void handlerOrder(Integer orderType) {
System.out.println("Normal order received");
}
}
// Split order strategy
@Component
public class SplitOrderStrategy extends ReceiveOrderTemplate {
@Override
public void handlerOrder(Integer orderType) {
System.out.println("Split order received");
}
}
</code>4. End‑to‑end implementation
The package structure is illustrated, and a REST controller demonstrates how the factory and template are used to process incoming requests.
<code>@RestController
@RequestMapping("/order")
public class ReceiveOrder {
@Resource
private ReceiveOrderFactory receiveOrderFactory;
@GetMapping("/get")
public String getOrder(Integer orderType) {
ReceiveOrderTemplate orderStrategy = receiveOrderFactory.getOrderStrategy(orderType);
orderStrategy.receiveOrder(orderType);
return "success";
}
}
</code>Running the service shows successful handling of different order types, and the architecture allows adding new channels simply by creating a new strategy class and updating the factory.
Overall, the combination of Template Method, Factory Method, and Strategy patterns provides a clear, maintainable, and extensible solution for multi‑channel order receipt in a fulfillment system.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
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.