- Forward


The Strategy Pattern
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Overview
Back SMYC Forward
  • The "Problem":
    • Have a family of interchangeable algorithms for accomplishing the same objective
  • Examples:
    • Line search algorithms in multi-dimensional optimization
    • Different text formatting algorithms (e.g., for paragraph formatting in a word processor)
    • Different metrics for finding the distance between, for example, colors
A Model of the Strategy Pattern
Back SMYC Forward

In UML

images/strategy_pattern.gif
An Example of the Strategy Pattern
Back SMYC Forward

In UML

images/strategy_example.gif
An Example of the Strategy Pattern (cont.)
Back SMYC Forward

The "Strategy" Interface

javaexamples/math/Metric.java
 
An Example of the Strategy Pattern (cont.)
Back SMYC Forward

Some Implementations

javaexamples/math/EuclideanMetric.java
 
An Example of the Strategy Pattern (cont.)
Back SMYC Forward

Some Implementations (cont.)

javaexamples/math/RectilinearMetric.java
 
An Example of the Strategy Pattern (cont.)
Back SMYC Forward

Some Implementations (cont.)

javaexamples/math/SupremumMetric.java
 
An Example of the Strategy Pattern (cont.)
Back SMYC Forward

The "Context"

javaexamples/strategy/Posterizer.java
 
There's Always More to Learn
Back -