...

/

Solution: Calculate Prices for Experiment Groups

Solution: Calculate Prices for Experiment Groups

Implement dynamic experiment-based pricing by selecting and applying pricing strategies at runtime through a configurable PricingService context.

We'll cover the following...

Solution explanation

  • Lines 2–22: We define three distinct pricing strategies: ConservativePricingAggressivePricing, and DynamicPricing.

    • Each exposes .calculate(basePrice) and returns a formatted numeric price.

    • ConservativePricing applies a 5% markup for stability.

    • AggressivePricing applies a 25% markup to maximize margin.

    • DynamicPricing introduces random variation, simulating algorithmic pricing experiments.

  • Lines 25–38: The PricingService class acts as the context.

    • It receives one strategy and delegates computation to it through .calculate().

    • .setStrategy() allows runtime replacement of pricing behavior.

    • This keeps the business workflow stable while isolating ...