Samuel Lee

Review of Strategy Pattern

Design Pattern: Elements of Reusable Object-Oriented Software by E. Gamma, et al. (1995)

Reviewed by Samuel Lee


Strategy Pattern

The Strategy pattern "define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it" (Gamma, 1995). As an example, there are many algorithms that exist today for breaking a stream of text into lines but hard-wiring all algorithms into the classes is not desirable because:

Strategy pattern avoid these problems by defining classes that encapsulate different algorithms.

Strategy pattern is used when:

The following is the Strategy pattern structure.

figure 1: Strategy pattern structure (Kremer, 2003)

The Strategy pattern consist of:

The consequences of Strategy pattern:

Strategy pattern is used by ET++ and InterViews to encapsulate different line-breaking algorithm. Borland's ObjectWindows also uses strategies in dialogs boxes to validate data that the user enters and ObjectWindows uses Validator objects to encapsulate validation strategies.

Critical Comments

I think Strategy pattern is an efficient mechanism to save continuously use of if / else statements or the switch statement. One of the main benefit is that when ever there is a new strategy, the programming code of strategy's containing class does not need to be changed. Another benefit is that strategies allows the strategy to be pay as you needed basis, unlike the switch/case where every switch/case target is hard-coded into the switch/case. Depends on the implementation, switch/case statements can be quite long which makes it hard to debug, but unlike switch/case statement, Strategy pattern provides a cleaner and simpler way of implementing the same conditions.


References
  1. Gamma, E., Helm, R., Johnson, R. & Vlissides, J. (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Reading Mass., Addison Wesley.
  2. Kremer, R. (2003), Programming Patterns Overview, SENG 609.04 Design Patterns. SERN, University of Calgary. http://sern.ucalgary.ca/courses/seng/609.04/W03/notes/index.html

The University of Calgary
back
Up to page above
up
forward to next page
forward
Software Engineering Research Network

mail to Samuel Lee

SENG 609.04

Last Modified Date: February 1, 2003