liveBook · Manning

Strategy design pattern

As explained in Object-Oriented Software Design in C++

The Strategy Design Pattern is a behavioral design pattern that encapsulates interchangeable algorithms behind a common interface so one can be selected independently of the client that uses it.

The Strategy Design Pattern organizes a family of algorithms behind common interfaces. A client selects interchangeable strategies at run time, so each algorithm can change independently of the client that uses it. section 8.2 section 8.2.3

A Strategy-based design typically provides:

  • A client class that aggregates strategy objects through interfaces rather than depending on concrete implementations. section 8.2.3
  • Separate strategy families when one client coordinates different variable behaviors, such as player recruitment and venue reservation. section 8.2 section 8.2.3
  • Runtime replacement of an algorithm, reducing hardcoded behavior and limiting changes when algorithms are added or modified. section 8.2 section 8.2.3

Sports teams and venues

Sport stores PlayerStrategy and VenueStrategy objects. Baseball supplies BaseballPlayers and Stadium; Football supplies FootballPlayers and Stadium; Volleyball supplies VolleyballPlayers and OpenField. Calling recruit_players() or reserve_venue() delegates to the selected strategy. Volleyball can later replace OpenField with Stadium without changing the Sport superclass. section 8.2.3

Figure 8.6 This version of the application is modeled from the Strategy Design Pattern for the sport teams and venues. The player family of algorithms implement the PlayerStrategy interface, and the venue family of algorithms implement the VenueStrategy interface. Class Sport uses has-a relationships to aggregate the algorithms. Each player and venue algorithm has a strategy() member function. The Baseball, Football, and Volleyball subclasses are now much simpler, and each one sets the player_strategy and venue_strategy member variables of the Sport superclass.

Use the class-and-interface diagram to trace the client, strategy interfaces, concrete strategies, and their has-a relationships. section 8.2.3

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains interfaces, composition, delegation, replacement, and trade-offs, but stops short of a library-based implementation.
Examples
Uses baseball, football, and volleyball strategies for recruiting players and reserving stadiums or open fields.
What it assumes
Assumes familiarity with design patterns and basic object-oriented relationships while contrasting composition with inheritance.
What you can do after
Build it from scratch by defining strategy interfaces, implementing algorithm variants, aggregating them in a client, and switching them at run time.
How it connects
Connects composition with inheritance, Code to the Interface, Encapsulates What Varies, Open-Closed, and Don't Repeat Yourself principles.

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 Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.