Applying Domain-Driven Design in a Pricing System: Architecture, Design, and Implementation

This article explains how Domain‑Driven Design (DDD) can be applied to a complex pricing system, covering business understanding, strategic and tactical design, context integration, layered architecture, code organization, and concrete Java examples that illustrate evaluation and pricing algorithms.

Architect
Architect
Architect
Applying Domain-Driven Design in a Pricing System: Architecture, Design, and Implementation

Domain‑Driven Design (DDD) provides a set of concepts and design approaches for building large, complex software systems, contrasting with traditional data‑driven design used in smaller projects.

The article describes the business domain of the ZhaiZhai pricing system, its workflow from device inspection reports to price calculation, and the need to abstract "scenarios" to handle different pricing contexts.

Strategic design identifies sub‑domains such as scenario, inspection report parsing, valuation item conversion, pricing flow, and pricing method, classifying them as generic, supporting, or core domains.

Tactical design defines bounded contexts for each sub‑domain, showing UML‑style diagrams where entities, value objects, and anti‑corruption layers are used to adapt external services.

Context integration is simplified to three relationships: collaborator, open host service/published language, and anti‑corruption layer, illustrated with integration diagrams.

The chosen architectural style is a loosely coupled layered architecture (API, Application, Domain, Infrastructure) with a common package for shared utilities, represented by the following project structure:

evaluation_sys
 - api
 - application
 - domain
 - infrastructure
 - common

Business logic is organized to avoid a monolithic service layer; instead, application services orchestrate domain services, and domain services encapsulate detailed logic, improving readability and maintainability.

public EvaluateResult eval(Scenario scenario, EvaluateContext context) {
    // obtain inspection report
    QcReport report = qcReportService.parseReport(context.getQcCode());
    // convert valuation items
    EvaluateItems evaluateItems = evaluateItemsService.transfer(report, scenario);
    // execute valuation process
    EvaluateResult result = evaluateProcessService.evaluate(scenario, context, evaluateItems);
    return result;
}

The valuation process service selects appropriate algorithms and executes the process, as shown in the following implementation:

public class EvaluateProcessService {
    public EvaluateResult evaluate(Scenario scenario, EvaluateContext context, EvaluateItems evaluateItems) {
        List<EvaluateAlgorithm> algorithms = EvaluateAlgorithmFactory.create(scenario);
        EvaluateProcess process = EvaluateProcessFactory.create(scenario, context, algorithms, evaluateItems);
        return process.evaluate();
    }
}

A concrete pricing algorithm (max‑price evaluation) iterates over all algorithms, calculates each price, and returns the highest result:

public class MaxPriceEvaluateProcess implements EvaluateProcess {
    private Scenario scenario;
    private EvaluateProcess context;
    private List<EvaluateAlgorithm> algorithms;
    private EvaluateItems evaluateItems;

    public EvaluateResult evaluate() {
        long maxPrice = 0;
        for (EvaluateAlgorithm algorithm : algorithms) {
            long price = algorithm.calculate(context, evaluateItems);
            if (maxPrice < price) {
                maxPrice = price;
            }
        }
        return new EvaluateResult(maxPrice);
    }
}

By decomposing logic into clear layers and bounded contexts, the system achieves high readability, testability, low learning cost, and easier maintenance, while adhering to DDD principles.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaDomain-Driven DesignDDD
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.