Strategy
Strategy makes a family of algorithms interchangeable behind one operation. The context delegates a decision to the selected strategy rather than branching over every concrete algorithm.
What you’ll understand
- Strategy makes a family of algorithms interchangeable behind one operation. The context delegates a decision to the selected strategy rather than branching over every concrete algorithm.
- Provide the selected fee strategy to checkout and delegate the variable calculation to it.
- Evaluate the tradeoff: Callers must choose an appropriate strategy, and extra objects can be excessive for a single simple rule. State changes behavior because of internal lifecycle; Strategy is chosen as a policy.
Useful first: Classes and Objects
The idea and when to recognize it
Strategy makes a family of algorithms interchangeable behind one operation. The context delegates a decision to the selected strategy rather than branching over every concrete algorithm.
- Requirement: Checkout needs a shipping price chosen from standard and express policies.
- Change to anticipate: Select a different pricing algorithm without altering the checkout total method.
- Design goal: Provide the selected fee strategy to checkout and delegate the variable calculation to it.
Start with the concrete problem
Checkout needs a shipping price chosen from standard and express policies.
Select a different pricing algorithm without altering the checkout total method.
| Direct implementation | Revised design |
|---|---|
| Embedding a growing list of carrier branches in checkout couples the context to every algorithm. | Provide the selected fee strategy to checkout and delegate the variable calculation to it. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | Callers must choose an appropriate strategy, and extra objects can be excessive for a single simple rule. State changes behavior because of internal lifecycle; Strategy is chosen as a policy. |
Responsibilities and contracts
Provide the selected fee strategy to checkout and delegate the variable calculation to it.
- Checkout: Combines prices through a stable operation. Public operations: total(items)
- ShippingPolicy: Hides fee selection. Public operations: fee()
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A route planner can switch between shortest-distance and lowest-toll strategies while retaining one planning interface.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
Callers must choose an appropriate strategy, and extra objects can be excessive for a single simple rule. State changes behavior because of internal lifecycle; Strategy is chosen as a policy.
Common mistake: Embedding a growing list of carrier branches in checkout couples the context to every algorithm.
- Explain the choice: Provide the selected fee strategy to checkout and delegate the variable calculation to it.
- Challenge your choice: Callers must choose an appropriate strategy, and extra objects can be excessive for a single simple rule. State changes behavior because of internal lifecycle; Strategy is chosen as a policy.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…