liveBook · Manning

Flexibility

As explained in Software Design for Python Programmers

Flexibility is the ability of software to change its behavior or accommodate new requirements without modifying its core implementation.

Flexibility is the ability to change software behavior or accommodate new requirements without modifying its core implementation. Hardcoded decisions bind run-time behavior to source code, so changes can require code edits, retesting, and documentation updates. Good design isolates likely-to-change parts so one modification does not leak across the application. section 8.2 section 1.7

Changing a sport’s venue strategy

A sports application initially hardcodes player-recruitment and venue algorithms into sport-specific classes. Refactoring those algorithms into PlayerStrategy and VenueStrategy implementations lets Sport delegate behavior through interfaces. Baseball, Football, and Volleyball can then select their initial strategies, while a Volleyball object can change its venue from OpenField to Stadium at run time. New algorithms can be added or modified independently, although the extra classes may be excessive for a very small application. section 8.2 section 8.2.3

A flexible design typically:

  • Encapsulates each variant of a behavior and makes interchangeable choices through an interface. section 8.2
  • Uses composition and delegation so a stable class can execute behavior supplied by strategy objects. section 8.2.3
  • Balances change tolerance against added complexity rather than pursuing unlimited flexibility. section 1.7
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() method. The Baseball, Football, and Volleyball subclasses are now much simpler, and each one sets the _SPORT_TYPE, _player_strategy, and _venue_strategy instance variables of the Sport superclass.
A diagram of a baseball player

AI-generated content may be incorrect.

Trace the strategy interfaces, aggregated algorithms, simplified sport subclasses, and replaceable venue behavior. section 8.2.3

How Software Design for Python Programmers covers it

How deep it goes
Explains hardcoding, change isolation, strategy replacement, and complexity tradeoffs through a worked sports refactoring.
Examples
Uses baseball, football, and volleyball strategies, including replacing volleyball’s open-field venue with a stadium.
What it assumes
Assumes beginning-to-intermediate Python, basic data structures, debugging, and object-oriented programming.
What you can do after
Apply it via the Strategy Design Pattern to add, modify, or replace algorithm variants with localized code changes.
How it connects
Connects flexibility to the Strategy Design Pattern, Encapsulates What Varies, Open-Closed, Delegation, and composition.

Also explained in

Compare how 9 books explain flexibility →

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.