Skip to main content
PreparationZone Logo
PrepZone

Strategy Pattern

Swap algorithms at runtime without touching the client.

Pass checkpoint below

Read these first

Why this matters

  • Interface PricingStrategy with price(Order)
  • CampusOS parking: nearest spot, cheapest spot, handicapped-first
  • Spring-style: inject strategy via constructor
  • Strategy: client chooses algorithm; often set once per operation
  • Patterns are forces and solutions — name the force before the pattern.
Creational
Factorychoose implementation
Builderstep-by-step assembly
Structural
Decoratorstack behaviour
Facadesimple entry point
Behavioral
Strategyswap algorithm
Observernotify subscribers
Statebehaviour by mode
Pick the family first, then the specific pattern.

Overview

Swap algorithms at runtime without touching the client. Study each section below in order — every list is interview-ready reference material.

Pricing strategies

Points to cover

  • Interface PricingStrategy with price(Order)
  • Implementations: Regular, StudentDiscount, PremiumMember
  • Client holds PricingStrategy field — set at runtime
  • New pricing rule = new class, zero edits to checkout flow

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • Spot assignment
  • Registration
  • vs State pattern

Test yourself

Answer these before moving on — recall is what makes it stick.