Re: Dynamic method?
howa wrote:
Hello,
I have a class, which has a method say, e.g.
int getMark(int x, int y) {
return x + y;
}
However, I want the getMark() method can be changed in runtime, maybe
change to...
int getMark(int x, int y) {
return x + 10 * y;
}
anyway...i want user to be able to change the behavior of this method
(change the mark calculation), but they don't need to recompile the
program everytime,
any method is recommended?
thanks.
Define an interface with getMark as the only method:
interface Marker{
int getMark(int x, int y);
}
Your class would have a Marker reference, and instead of calling
getMark directly would call e.g. marker.getMark(x,y).
If there is a default Marker initialize accordingly:
Marker marker = new Marker(){
public int getMark(int x, int y){
return x + y;
}
};
You can use a Marker as a constructor parameter, and/or provide a
setMarker method to change it for an existing object.
Patricia