Posted By:
Stephen_McConnell
Posted On:
Monday, September 1, 2003 05:23 AM
Usually, if you define an interface with a particular method on it that is overriden by the implementing class then you are using the "strategy" design pattern.
Your code would go something like this.
public interface GoLeftYoungMan{
boolean goLeft(int distance);
}
public class Car implements GoLeftYoungMan{
...... bunches of Car code....
public boolean goLeft(int distance){
boolean madeIt = false;
/// code to turn the wheel
/// code to turn the car
/// code to move the car so much "distance"
return madeIt;
}
}
public class Dog implements GoLeftYoungMan{
..... bunches of Dog code .....
public boolean goLeft(int distance){
boolean madeIt = false;
/// Code to look both ways.
/// Code to see the nearest fire hydrant
/// Code to move the the back end around.
/// Code to change to front end direction
/// Code to go the distance
madeIt = true;
return madeIt;
}
}
public class TeenAger implements GoLeftYoungMan{
.... small amount of teenager code ....
public boolean goLeft(int distance){
boolean madeIt = false;
// Code to turn to the right.....
return madeIt;
}
public class MoverClass{
public boolean negotiatePath(GoLeftYoungMan negotiate){
.... Code to calculate distance...
return negotiate.goLeft(distance);
}
}
So, you see that you pass in the Implementing class by casting to the interface.... Then, all you do is execute the over-ridden method. The concrete implementation of the method or methods is what determines which "Strategy" is being used.
Hope this helps.
Stephen McConnell