CSC/ECE 517 Fall 2007/wiki1b 8 ktrk: Difference between revisions
No edit summary |
No edit summary |
||
Line 5: | Line 5: | ||
===When to Use the Strategy Pattern=== | ===When to Use the Strategy Pattern=== | ||
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled "code smells" by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include: | |||
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled "code smells" by Martin Fowler in Refactoring) are described where the strategy design pattern could be used to architect a better solution. These include: | |||
<p>1. A class that exhibits many behaviors</p> | <p>1. A class that exhibits many behaviors</p> | ||
<p>2. A class that uses many variations of an algorithm</p> | <p>2. A class that uses many variations of an algorithm</p> | ||
Line 13: | Line 12: | ||
===Advantages of the Strategy Pattern=== | ===Advantages of the Strategy Pattern=== | ||
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code. | |||
<p>1. Allows classes to change behavior at runtime or design time</p> | <p>1. Allows classes to change behavior at runtime or design time</p> | ||
<p>2. Decreases code duplication among classes using variations of the same behavior</p> | <p>2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]</p> | ||
<p>3. Behavior is better encapsulated by not being buried in its context</p> | <p>3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]</p> | ||
<p>4. A well known design pattern communicates the intent of the code more readily</p> | |||
===Similar Patterns=== | |||
<p>State, Command, and Bridge</p> | |||
<p>Use the state pattern when the state of the object changes with a change in behavior.</p> | |||
<p>Use the bridge pattern when a structural design is needed.</p> | |||
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern]. | |||
==Strategy Pattern Implementation in Java and Ruby== | ==Strategy Pattern Implementation in Java and Ruby== | ||
==Comparison of Implementations== | ==Comparison of Implementations== | ||
==References== | ==References== | ||
http://en.wikipedia.org/wiki/Strategy_pattern | |||
<p>Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995</p> | |||
<p>Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999</p> | |||
<p>http://en.wikipedia.org/wiki/Strategy_pattern</p> |
Revision as of 03:04, 1 October 2007
Introduction
What is the Strategy Pattern?
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.
When to Use the Strategy Pattern
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled "code smells" by Martin Fowler in Refactoring) are described where the strategy design pattern could be used to architect a better solution. These include:
1. A class that exhibits many behaviors
2. A class that uses many variations of an algorithm
3. A class that uses data structures related to a behavior but not the class
4. Many similar classes that differ only in a type of behavior
Advantages of the Strategy Pattern
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.
1. Allows classes to change behavior at runtime or design time
2. Decreases code duplication among classes using variations of the same behavior [1]
3. Behavior is better encapsulated by not being buried in its context [2]
4. A well known design pattern communicates the intent of the code more readily
Similar Patterns
State, Command, and Bridge
Use the state pattern when the state of the object changes with a change in behavior.
Use the bridge pattern when a structural design is needed.
For a discussion of the strategy pattern compared to the state and bridge patterns see Strategy Pattern vs. Bridge Pattern.
Strategy Pattern Implementation in Java and Ruby
Comparison of Implementations
References
Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995
Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999