liveBook · Manning

Strategy design pattern

As explained in Software Design for Python Programmers

A strategy is an encapsulated algorithm that provides interchangeable behavior for a client.

The Strategy Design Pattern encapsulates a family of algorithms and makes them interchangeable at run time. A client delegates behavior to strategy objects instead of hardcoding algorithm variants. section 8.2

Inheritance-based designs place recruiting and venue algorithms in each Sport subclass, creating duplicated code, weak reuse, hardcoded behavior, and change leaks. Composition moves those algorithms into separate strategy families. section 8.2.2 section 8.2.3

A practical Strategy design uses the following structure:

  • Define interfaces such as PlayerStrategy and VenueStrategy, each exposing a strategy() operation. section 8.2.3
  • Implement interchangeable algorithms such as BaseballPlayers, FootballPlayers, VolleyballPlayers, Stadium, and OpenField. section 8.2.3 section 8.2.4
  • Give the client has-a references to strategy objects, then delegate operations such as recruit_players() and reserve_venue(). section 8.2.3
  • Select or replace strategies at run time when behavior must vary without changing the client. section 8.2 section 8.2.3

Sports, players, and venues

A Baseball object can use BaseballPlayers and Stadium, while Volleyball can use VolleyballPlayers and OpenField. Replacing volleyball.venue_strategy with Stadium changes its venue without modifying Volleyball or Sport. Shared Stadium behavior is implemented once and reused by baseball and football. section 8.2.3

A black background with a black square

Description automatically generated with medium confidence

Use the architecture figure to compare the client, strategy interfaces, concrete algorithms, and has-a relationships. section 8.2

Strategy favors composition over inheritance: the client remains loosely coupled to complete algorithms, which can be added, removed, or updated independently. Template Method instead uses inheritance to preserve an algorithm skeleton while subclasses redefine selected steps. section 8.3

How Software Design for Python Programmers covers it

How deep it goes
Works from inheritance problems through interfaces, composition, delegation, setters, concrete strategies, and runtime replacement.
Examples
Demonstrates sports teams using player-recruitment and venue strategies, including replacing volleyball's open field with a stadium.
What it assumes
Assumes basic familiarity with design patterns, inheritance, interfaces, and composition.
What you can do after
Build it from scratch by separating algorithm families behind interfaces and selecting or replacing implementations at runtime.
How it connects
Connects composition, inheritance, code to the interface, encapsulation, Open-Closed, Don't Repeat Yourself, and Template Method.

Also explained in

Compare how 2 books explain strategy design pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.