Dynamic method?

H

howa

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.
 
P

Patricia Shanahan

howa said:
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
 
S

steph

howa said:
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.

If you not concern with performance, you can use a java scripting
language such as Beanshell (http://www.beanshell.org/), and do sth like:


int getMark(int x, int y)
{
Context context= new Context();
context.put("x", new Integer(x));
context.put("y", new Integer(y));
return (int) Beanshell.interpret(context, "some formula involving x and
y");
}
 
T

tzvika.barenholz

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.

Your best bet is the Strategy Design Pattern (http://en.wikipedia.org/
wiki/Strategy_pattern)

T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top