How gaossr/onepro works
Project type and purpose
• This is a Java-based, in-memory, algorithm-driven “discount stacking” library with a small Spring-boot demo. Its purpose is to take a set of goods and a set of discount rules, explore all valid application orders (permutations), and return the globally optimal discount combination.
Main components
a. API / Demo Layer (blue)
• TestController (com.gch.discount.demo) – mocks input, invokes the pipeline
• DiscountGroupUtil – parses “share/exclude” group protocols into sets of DiscountWrapper
b. Orchestration Layer (green)
• Flowable – builder/DSL that wires CalculatorRouter and triggers Permutation.perm()
• CalculatorRouter – routes each DiscountWrapper.type to the correct Calculator implementation
c. Core Algorithm Layer (orange)
• Permutation<T> – pre-computes permutations up to SUPPORTEDSIZE, manages an Aₙ³ cache (Map<Integer,CalcState>)
• DiscountContext<T> – holds items, wrappers, preCompute results, and accumulates CalcStage[] and CalcResult
• CalcState – snapshot of partially applied permutation (first 3 steps) for cache reuse
d. Plugin & Extension Layer (purple)
• AbstractCalculator<T> & Calculator<T> – template method defining calcWarp() and abstract calc()
• Concrete calculators (ManjianCalc, ZhekouCalc) – @Component("type") beans implementing specific discount logic
• PreCompute<T> interface & PreComputeHolder – reflection-based loading of custom pre-computation strategies
e. Utilities & Config (grey)
• IdGenerator, LimitingUtil – ID generation and performance counters
• calculator-core.properties – defines precompute.path for scanning PreCompute implementations
Relationships & data flow
1. Client/TestController builds: List<GoodsItem> + List<DiscountWrapper>
2. → DiscountGroupUtil.transform() → List<Pair<Set<DiscountWrapper>,Set<DiscountWrapper>>>
3. For each “left” set:
a. DiscountContext.create(totalPrice, items, wrappers)
• runs PreCompute implementations, populates context.preCompute map
b. Flowable.perm(context)
• calls Permutation.permute(wrapperList, context)
• Permutation uses static PERMUTATIONS and its cache
• for each permutation step, calls CalculatorRouter.route(type).calcWarp(...)
• CalculatorRouter selects the correct AbstractCalculator → invokes calc()
• calc() modifies DiscountContext.calcResult and records CalcStage
4. After exploring all, TestController picks the lowest final price
Architectural patterns & principles
• Template Method: AbstractCalculator defines calcWarp() and delegates to calc()
• Strategy / Router: CalculatorRouter + @Component beans for each discount type
• Builder / Fluent API: Flowable.build(calculatorRouter).perm(ctx)
• Caching Optimization: static pre-computed permutations + Aₙ³ step-level cache
• Reflection-based plugin loading: PreComputeHolder scans precompute.path