Mastering Complex Business Code: Structured Decomposition & Domain Modeling
This article explains how to tackle intricate business logic by applying a top‑down structured decomposition, combining it with bottom‑up domain modeling, and using simple composition patterns, illustrated with a product‑on‑sale use case and concrete Java code examples.
A Complex Business Process
Zhang Jianfei, a senior technical expert at Alibaba, shares a methodology for writing complex business code, focusing on structured decomposition and object‑oriented analysis.
Business Background
Retail‑Connect supplies offline stores via a B2B model, aiming to digitize and streamline traditional supply‑chain channels. Alibaba acts as the platform providing service functions.
Process Decomposition
The on‑sale operation is a critical step that involves many data validations and associations. A simplified flow diagram (omitted) shows the steps.
Rather than placing all logic in a single service method, the author advocates a divide‑and‑conquer approach, creating distinct phases and steps.
Code Example: On‑Sale Command
@Command
public class OnSaleNormalItemCmdExe {
@Resource
private OnSaleContextInitPhase onSaleContextInitPhase;
@Resource
private OnSaleDataCheckPhase onSaleDataCheckPhase;
@Resource
private OnSaleProcessPhase onSaleProcessPhase;
@Override
public Response execute(OnSaleNormalItemCmd cmd) {
OnSaleContext onSaleContext = init(cmd);
checkData(onSaleContext);
process(onSaleContext);
return Response.buildSuccess();
}
private OnSaleContext init(OnSaleNormalItemCmd cmd) {
return onSaleContextInitPhase.init(cmd);
}
private void checkData(OnSaleContext onSaleContext) {
onSaleDataCheckPhase.check(onSaleContext);
}
private void process(OnSaleContext onSaleContext) {
onSaleProcessPhase.process(onSaleContext);
}
}Phase Example: On‑Sale Process Phase
@Phase
public class OnSaleProcessPhase {
@Resource
private PublishOfferStep publishOfferStep;
@Resource
private BackOfferBindStep backOfferBindStep;
// other steps omitted
public void process(OnSaleContext onSaleContext) {
SupplierItem supplierItem = onSaleContext.getSupplierItem();
generateOfferGroupNo(supplierItem);
publishOffer(supplierItem);
bindBackOfferStock(supplierItem);
syncStockRoute(supplierItem);
setVirtualProductExtension(supplierItem);
markSendProtection(supplierItem);
recordChangeDetail(supplierItem);
syncSupplyPriceToBackOffer(supplierItem);
setCombineProductExtension(supplierItem);
removeSellOutTag(offerId);
fireDomainEvent(supplierItem);
closeIssues(supplierItem);
}
}The author emphasizes that a simple composed‑method pattern suffices; no heavyweight workflow engine or excessive design patterns are needed.
Process Decomposition + Object Model
Introducing domain models reduces conditional logic. For example, checking inventory for combined products becomes a straightforward method call on the model.
if (backOffer.isCloudWarehouse()) {
return;
}
if (backOffer.isNonInWarehouse()) {
throw new BizException("Please contact operations to establish warehouse relationship!");
}
if (backOffer.getStockAmount() < 1) {
throw new BizException("Real‑stock must be greater than 0. Check replenishment. [id:" + backOffer.getSupplierItem().getCspuCode() + "]");
}Using inheritance (e.g., CombineBackOffer extends BackOffer) eliminates most if‑else branches.
Methodology: Structured Decomposition + Object‑Oriented Analysis
The recommended approach combines top‑down process breakdown with bottom‑up domain modeling, iteratively refining the system.
Capability Sinking
Rather than forcing all business logic into the Domain layer, only reusable capabilities are abstracted there; the rest stays in the application layer’s use cases.
Guidelines for Sinking
Reusability : When duplicate code appears, consider sinking.
Cohesion : Place functionality in the most appropriate entity or service.
Examples include moving product‑unit checks into the CSPU model:
public class CSPU {
private String code;
private String baseCode;
// ...
public boolean isMinimumUnit() {
return StringUtils.equals(code, baseCode);
}
public boolean isMidPackage() {
return StringUtils.equals(code, midPackageCode);
}
}Conclusion
The article summarizes the author's thoughts on handling complex business scenarios, extending the COLA architecture, and encourages developers to strengthen foundational, OO, and modeling skills.
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.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
