Javabeans with calculables or derivables properties
Recently I got into a scenario in a web app where I have a java bean that
has an additional attribute that is calculable based on other attributes.
For example, I could be tempted to write this javabean (done like this for
the sake of simplicity and clarity).
class DTO {
private int a;
private int b;
// setters and getters for a and b
public int dif () {
return a - b;
}
}
The first question: is a bad thing to add some behavior in a bean?
Seconds question and more tecnical related to spring mvc: if I want to
display inside a jsp the result of that method, how could I do that?
Through a google search I reached several ways to achieve it.
Using sciptlets
Using spring:eval tag
Using the lastest version of EL
Because of application architecture, I cannot use nor 2 or 3.
I also thought on create the property calculable as another attribute and
in the get accessor and the behaviour to calculate. But i guess is not a
good approach due to the simple fact that a "get" is an method for
accessing values rather for calculating values. Two diferents things, and
a method should always do what is says to do.
No comments:
Post a Comment