Object-Oriented Programming

Design Pattern: Strategy

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Strategy

  • Behavioral Pattern

  • Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

  • AKA: Policy

Strategy: Motivation

motivation

Strategy: Motivation

  • Multiple ways to break text into lines
  • Hardcoding into the clients is difficult:
    • If you add the line-breaking code into the Client it becomes more complex, especially with multiple algorithms
    • Different algorithms are appropriate at different times and don’t want to support them all
    • When the client directly contains the line-breaking code, it is difficult to add new algorithms and change existing

Strategy: Motivation

motivation2

Strategy: Motivation

Strategy: Motivation

gofmotivation

Strategy: Motivation Roles

  • Composition class - maintains and updates line breaks of text
  • Line-breaking strategies are implemented by subclasses of the (abstract) Compositor class
    • SimpleCompositor - a simple strategy for determining line breaks
    • TeXCompositor - implements the TEX algorithm that optimizes line breaks an entire paragraph at a time
    • ArrayCompositor - implements a strategy so that each row has a fixed number of items

Strategy: Applicability

  • Many related classes differ only in behavior
  • Different variants of an algorithm are needed, often for different space/time tradeoffs
  • The algorithm uses data the client should not know about, or had dependencies we want to leave out of the client
  • A class has many behaviors, and there are multiple conditional statements in the operations. Move the switch statement into its own Strategy class

Strategy: Structure

gofstructure

Strategy: Participants

  • Strategy (e.g., Compositor)
    • Declares an interface common to all supported algorithms
    • Context uses this interface to call the algorithm defined by a ConcreteStrategy
  • ConcreteStrategy (e.g., SimpleCompositor, TeXCompositor, ArrayCompositor)
    • Implements the algorithm using the Strategy interface
  • Context (e.g., Composition)
    • Configured with a ConcreteStrategy object
    • Maintains a reference to a Strategy object
    • May define an interface that lets Strategy access its data

Strategy: Collaborations

  • Strategy and Context interact to implement the chosen algorithm:
    • A Context passes data required by the algorithm to the Strategy in the call
    • A Context can pass itself as an argument to Strategy operations
  • A Context forwards requests from its clients to its Strategy
    • Clients usually create and pass a ConcreteStrategy to the context
    • Typically the client chooses from a family of ConcreteStrategy classes

Strategy: Advantages

  • Families of related algorithms
  • An alternative to subclassing
  • Eliminate conditional statements
  • Choice of implementation

Strategy: Disadvantages

  • Clients must pick which algorithm to use
  • Communication overhead between Strategy and Context
  • Increases the number of objects

Implementation

Known Uses

  • RTL System for compiler code optimization [JML92]- strategies define different register allocation schemes (RegisterAllocator) and instruction set scheduling policies (RISCscheduler, CISCscheduler)
  • ET++SwapsManager calculation engine framework computes prices for different financial instruments [EG92]
    • ConcreteStrategy classes for generating cash flows, valuing swaps, and calculating discount factors

Related Patterns

  • Template Method
  • Flyweight