CSC/ECE 517 Fall 2010/ch3 3h PW: Difference between revisions
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
<pre> | <pre> | ||
interface Action { | interface Action { | ||
void perform(); | |||
} | } | ||
Line 16: | Line 16: | ||
class Dribble implements Action { | class Dribble implements Action { | ||
public | public void perform() { | ||
System.out.println("Player is dribbling"); | System.out.println("Player is dribbling"); | ||
} | } | ||
Line 23: | Line 23: | ||
class Pass implements Action { | class Pass implements Action { | ||
public void perform() { | |||
System.out.println("Player is passing"); | System.out.println("Player is passing"); | ||
} | } | ||
Line 30: | Line 30: | ||
class Shoot implements Action { | class Shoot implements Action { | ||
public | public void perform() { | ||
System.out.println("Player is shooting"); | System.out.println("Player is shooting"); | ||
} | } | ||
Line 45: | Line 45: | ||
} | } | ||
public | public void perform() { | ||
action.perform(); | |||
} | } | ||
} | } | ||
Line 61: | Line 61: | ||
bballPlayer.perform(); | bballPlayer.perform(); | ||
bballPlayer = new | bballPlayer = new BasketBallPlayer(new Pass()); | ||
bballPlayer.perform(); | bballPlayer.perform(); | ||
bballPlayer = new | bballPlayer = new BasketBallPlayer(new Shoot()); | ||
bballPlayer.perform(); | bballPlayer.perform(); | ||
} | } |
Revision as of 23:31, 5 October 2010
The Strategy pattern in static and dynamic languages
What is the Strategy Pattern
The main purpose of the strategy pattern is to separate out an object and how it behaves into two different classes. The first advantage for doing this is so that if an object can have different behaviors then it is easy to manage them in their own classes. Secondly, if a certain type of behavior need to be changed, it only need to be changed in that behavior class rather than in multiple classes if there were multiple objects that had this behavior.
How to Implement in a Static Language
Example
Java Code
interface Action { void perform(); } // Implements the algorithm using the strategy interface class Dribble implements Action { public void perform() { System.out.println("Player is dribbling"); } } class Pass implements Action { public void perform() { System.out.println("Player is passing"); } } class Shoot implements Action { public void perform() { System.out.println("Player is shooting"); } } // Configured with a ConcreteStrategy object and maintains a reference to a Strategy object class BasketBallPlayer { private Action action; // Constructor public BasketBallPlayer(Action action) { this.action = action; } public void perform() { action.perform(); } } //BasketBallPlayerExample test application class BasketBallPlayerExample { public static void main(String[] args) { BasketBallPlayer bballPlayer; // Three contexts following different strategies bballPlayer = new BasketBallPlayer(new Dribble()); bballPlayer.perform(); bballPlayer = new BasketBallPlayer(new Pass()); bballPlayer.perform(); bballPlayer = new BasketBallPlayer(new Shoot()); bballPlayer.perform(); } }
How to Implement in a Dynamic Language
Examples
Ruby code