Here's a thought... It seems to me that processRequest is most often used
as an indication for the bean to "do its work". In other words, the bean
expects a bunch of set* calls, a processRequest call (where the work is
done), and then a bunch of get* calls.
Now, if you always knew the exact order that the set* and get* calls were
to come, you could do the work on the first get* call. However, that's
not very robust. How about keeping a little state information. On each
get* call, if the work hasn't been done, it's done before the get is done.
Then on each set* call, the state is reset (especially handy if the bean
is reused). This might even be called a 'design pattern.'
Here's an example:
public abstract class WorkerBean {
private boolean workDone = false;
protected abstract void doWork();
protected final void maybeDoWork() {
if (!workDone) {
doWork();
workDone = true;
}
}
protected abstract void resetWork();
protected final void maybeResetWork() {
if (workDone) {
resetWork();
workDone = false;
}
}
}
public class DividerBean extends WorkerBean {
private float dividend;
private float divisor;
private float quotient;
private float remainder;
public void setDividend(float dividend) {
maybeResetWork();
this.dividend = dividend;
}
public float getDividend() {
return dividend;
}
public void setDivisor(float divisor) {
maybeResetWork();
this.divisor = divisor;
}
public float getDivisor() {
return divisor;
}
protected void doWork() {
quotient = dividend / divisor;
remainder = dividend % divisor;
}
public float getQuotient() {
maybeDoWork();
return quotient;
}
public float getRemainder() {
maybeDoWork();
return remainder;
}
protected void resetWork() {
dividend = 0;
divisor = 0;
quotient = 0;
remainder = 0;
}
}
public class Test {
public static void main(String [] args) {
DividerBean db = new DividerBean();
db.setDividend(33);
db.setDivisor(10);
System.out.println("Dividend = " + db.getDividend());
System.out.println("Divisor = " + db.getDivisor());
System.out.println("Quotient = " + db.getQuotient());
System.out.println("Remainder = " + db.getRemainder());
db.setDivisor(3);
db.setDividend(1);
System.out.println("Dividend = " + db.getDividend());
System.out.println("Divisor = " + db.getDivisor());
System.out.println("Remainder = " + db.getRemainder());
System.out.println("Quotient = " + db.getQuotient());
}
}
Notes:
- An even more robust bean might keep track of which set* calls had been
made and throw an IllegalStateException if any required ones had not been
called.
- Any exceptions thrown while doing work (like ArithmeticException in this
case) would be thrown in whichever get* method was called first. It might
make some sense to add some machinery in doWork to capture and
save exceptions and then provide boolean getExceptionOccured() and
Throwable getException() methods.
John K Peterson -- IT Services Development -- Brigham Young University
Internet: [EMAIL PROTECTED] Phone: (801) 378-5007
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".