← → or space · progress saves for Continue on the roadmap
Goal
Add new behavior by introducing new types instead of editing a growing switch.
Idea
- Core flow stays stable: “compute price,” “send notification,” “score risk.”
- Variations live in pluggable strategies (
implementsa small interface).
Step shape
abstract class PricingStrategy {
double quote(double base);
}
class StandardPricing implements PricingStrategy {
@override
double quote(double base) => base;
}
class MemberPricing implements PricingStrategy {
@override
double quote(double base) => base * 0.9;
}
class Checkout {
final PricingStrategy pricing;
Checkout(this.pricing);
double total(double base) => pricing.quote(base);
}- New member tier? Add
class VipPricing implements PricingStrategyinstead of editingCheckoutinternals.
Practice tasks
- Replace a three-branch
ifon an enum with aMap<Role, PricingStrategy>or a small registry. - Identify one
switchthat grows every sprint; sketch Strategy types for two branches only. - Note when O is overkill: two branches that never grow can stay as
if.