Here is the scenario,
Let's say I have a user class like so:
public class User{
private String firstName;
private String lastName;
//...
// setter, getters
}
Then I have a class like so to handle user Comments:
public class Comments{
// some fields
public static loadComments(User user, int count){...}
}
So far very basic stuff. However, I want to add some helpers of course to make it easier load comments for user. So I could create something in User class:
final static int defaultCount = 10;
...
public Comment comments(){
return Comments.loadComments(this, defaultCount);
}
I think this is an easy way to not have to pass around user instances around. But at this point, I am unhappy because I have coupled my user bean object with business logic that loads the comment. I have also saved the default count in user class which shouldn't belong there. So what is the best way to do this? My goal is to pass this object to a jsp so that JSTL functions can be called. I had an idea to create a UserWrapper which would look like this...
public class UserWrapper{
private final static defaultCount = 10;
private final User user;
public UserWrapper(User user){
this.user = user;
}
// should probably cache this but i am not going to show this for simplicity
public Comments getComments(){return Comments.loadComments(user, 10);}
}
I hope I was clear. I don't like using useBean tag because its just not necessary for something like this. I am hoping there is a cleaner way to approach something like this! Any help would be appreciated!
Edit: One thing I forgot to mention. I want to be able to use this code in JSTL. Which means it must be a getter. The DAO model is well known but it doesn't help too much when my front-end developer needs to write a scriplet or I need load it for places that he may or may not need.