Skip to main content

Strategy and Template Method Lab

Retrieval Prompts

  1. State the three roles in Strategy and what each knows about the others.
  2. State the difference between a Strategy object and a function reference, and when each is preferable.
  3. State the difference between Strategy and Template Method in one sentence.
  4. State what the "template method" itself is allowed to do and not to do.
  5. State one case where introducing Strategy is over-engineering.

Compare and Distinguish

Separate these pairs clearly:

  • Strategy versus Template Method
  • Strategy object versus closure
  • "Replace Conditional with Polymorphism" versus a match/switch on an enum
  • interface with one method versus a function type
  • hook method versus abstract method

Common Mistake Check

Identify the error in each:

  1. "I extracted Strategy for the one discount formula we have. Done."
  2. "The context has if strategy instanceof FreeShipping to log a special case. It's fine -- it's only one line."
  3. "All our AI bots subclass GameAI, but each overrides turn() to change the step order."
  4. "Every validator has validate(input): bool and no state; we made each one a class because it feels cleaner."
  5. "Our sort takes a list of comparator objects. When we need a second-level tie-breaker we add a boolean flag to the interface."

Mini Application

For each scenario:

  1. Decide: Strategy, Template Method, plain function, or none.
  2. Sketch the interface.
  3. Name the one axis of variation in one phrase.
  4. Note one simpler alternative.

Scenarios:

  1. Password hashing where the algorithm may switch from bcrypt to argon2 without redeploy.
  2. Pricing engine where 80% of the algorithm is identical and only two steps differ by product category.
  3. Validating a user-submitted form field. Ten rule types.
  4. Ordering a list of items by any user-chosen column.
  5. Image filter pipeline where the pipeline order is fixed but individual filters vary.

Refactor Drill

Take this switch-on-type function and refactor it to Strategy, then to closure-based if appropriate:

def fee(kind, amount):
if kind == "basic": return 0.00
if kind == "silver": return amount * 0.02
if kind == "gold": return max(1.00, amount * 0.015)
if kind == "vip": return 0.00
raise ValueError(kind)

Target: call sites compute fee through an interface; adding a sixth tier requires no change to existing tiers or the caller.

Evidence Check

This page is complete only if you can justify each pattern choice by naming the axis of variation and the simpler alternative you rejected.