CSC/ECE 517 Fall 2010/ch3 3h PW: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 5: Line 5:


==How to Implement in a Static Language==
==How to Implement in a Static Language==
The static languages implement the Strategy Pattern by creating a class and inside of that class is a reference to an action class.  This action class is usually a Superclass, that way one of the actual types of actions to be perform can be assigned.  These actions are essentially subclasses of the action class.  In java, we can use the abstract type interface[http://en.wikipedia.org/wiki/Interface_(Java)] which means any class that implements it must define each of the abstract functions.
The static languages implement the Strategy Pattern by creating a class and inside of that class is a reference to an action class.  This action class is usually a Superclass, that way one of the actual types of actions to be perform can be assigned.  These actions are essentially subclasses of the action class.  In java, we can use the abstract type interface[http://en.wikipedia.org/wiki/Interface_(Java)] which means any class that implements it must define each of the abstract functions. In the example below, the main object is the BasketballPlayer class. In that class is a reference to the Action interface.  The action interface has an abstract method perform.  There are 3 classes that implement the Action class.  They are Pass, Dribble, and Shoot.  Each of these methods defines the method perform and then does that action.  The BasketballPlayerExample class shows how to instantiate the BasketballPlayer class and then define different actions for it.


===Example===
===Example===
Line 38: Line 38:
   
   
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object
class BasketBallPlayer {
class BasketballPlayer {
   
   
     private Action action;
     private Action action;
   
   
     // Constructor
     // Constructor
     public BasketBallPlayer(Action action) {
     public BasketballPlayer(Action action) {
         this.action = action;
         this.action = action;
     }
     }
Line 51: Line 51:
     }
     }
}
}
//BasketBallPlayerExample test application
//BasketballPlayerExample test application
   
   
class BasketBallPlayerExample {
class BasketballPlayerExample {
   
   
     public static void main(String[] args) {
     public static void main(String[] args) {
   
   
         BasketBallPlayer bballPlayer;
         BasketballPlayer bballPlayer;
   
   
         // Three contexts following different strategies
         // Three contexts following different strategies
         bballPlayer = new BasketBallPlayer(new Dribble());
         bballPlayer = new BasketballPlayer(new Dribble());
         bballPlayer.perform();
         bballPlayer.perform();
   
   
         bballPlayer = new BasketBallPlayer(new Pass());
         bballPlayer = new BasketballPlayer(new Pass());
         bballPlayer.perform();
         bballPlayer.perform();
   
   
         bballPlayer = new BasketBallPlayer(new Shoot());
         bballPlayer = new BasketballPlayer(new Shoot());
         bballPlayer.perform();
         bballPlayer.perform();
     }
     }

Revision as of 23:49, 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

The static languages implement the Strategy Pattern by creating a class and inside of that class is a reference to an action class. This action class is usually a Superclass, that way one of the actual types of actions to be perform can be assigned. These actions are essentially subclasses of the action class. In java, we can use the abstract type interface[1] which means any class that implements it must define each of the abstract functions. In the example below, the main object is the BasketballPlayer class. In that class is a reference to the Action interface. The action interface has an abstract method perform. There are 3 classes that implement the Action class. They are Pass, Dribble, and Shoot. Each of these methods defines the method perform and then does that action. The BasketballPlayerExample class shows how to instantiate the BasketballPlayer class and then define different actions for it.

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

class BasketballPlayer
  def initialize(&action)
    @action = action
  end
  def perform 
    @move.call
  end
  def method_missing(method_name)
    puts "#{action} is not a known b-ball play"
  end
end

module Pass
  def pass
    puts"Player is passing!"
  end
end

module Dribble
  def dribble
    puts "Player is dribbling!"
  end
end

module Shoot
  def shoot
    puts "Player is shooting!"
  end 
end

bballPlayer = BasketballPlayer.new(dribble)
offense.perform #=>Player is dribbling!

bballPlayer = BasketballPlayer.new(pass)
defense.perform #=>Player is passing!

bballPlayer = BasketballPlayer.new(shoot)
offense.perform #=>Player is passing!
...

Ruby Code using Blocks

class BasketballPlayer
  def initialize(&action)
    @action = action
  end
 
  def perform
    @action.call
  end
end

a = BasketballPlayer.new { puts 'Player is passing!' }
a.perform #=> Player is passing!

b = BasketballPlayer.new { puts 'Player is dribbling!' }
b.execute #=> Player is dribbling!

c = BasketballPlayer.new { puts 'Player is shooting!' }
c.perform #=> Player is shooting!