Exercise: Strategy
This exercise puts the Strategy pattern into code.
Scenario
A Calculator collects integers and applies a reduction when asked. The active reduction
algorithm can be changed at any time. Three algorithms are supported: sum, max, and min.
The default before any strategy command is sum. If no numbers have been pushed, any
strategy returns 0.
Commands
| Command | Behavior | Output |
|---|---|---|
["push", n] | Append the integer n to the list | (none) |
["strategy", name] | Set the active strategy to "sum", "max", or "min" | (none) |
["run"] | Apply the active strategy over all pushed numbers | the integer result as a string |
7 run push 5 push 3 push 8 run strategy max run
0 16 8
Your task
Create SumStrategy, MaxStrategy, and MinStrategy (each implements the ReductionStrategy base) so the commands produce the output described above. The starter keeps the ReductionStrategy base, the Calculator, and the registry, and marks where to add the three strategy classes with a TODO comment listing each class's method signature and behavior.