CSC/ECE 517 Fall 2010/ch3 3h PW: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 7: | Line 7: | ||
===Example=== | ===Example=== | ||
Java Code | Java Code | ||
<pre> | |||
interface Action { | |||
int perfom(); | |||
} | |||
// Implements the algorithm using the strategy interface | |||
class Dribble implements Action { | |||
public int perform() { | |||
System.out.println("Player is dribbling"); | |||
} | |||
} | |||
class Pass implements Action { | |||
public int perform() { | |||
System.out.println("Player is passing"); | |||
} | |||
} | |||
class Shoot implements Action { | |||
public int 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 int perform() { | |||
return 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 Context(new Pass()); | |||
bballPlayer.perform(); | |||
bballPlayer = new Context(new Shoot()); | |||
bballPlayer.perform(); | |||
} | |||
} | |||
</pre> | |||
==How to Implement in a Dynamic Language== | ==How to Implement in a Dynamic Language== |
Revision as of 23:19, 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 { int perfom(); } // Implements the algorithm using the strategy interface class Dribble implements Action { public int perform() { System.out.println("Player is dribbling"); } } class Pass implements Action { public int perform() { System.out.println("Player is passing"); } } class Shoot implements Action { public int 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 int perform() { return 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 Context(new Pass()); bballPlayer.perform(); bballPlayer = new Context(new Shoot()); bballPlayer.perform(); } }
How to Implement in a Dynamic Language
Examples
Ruby code