Java - Is this a bad design pattern?
Posted
by Walter White
on Stack Overflow
See other posts from Stack Overflow
or by Walter White
Published on 2010-04-01T14:14:23Z
Indexed on
2010/04/01
14:23 UTC
Read the original article
Hit count: 472
Hi all,
In our application, I have seen code written like this:
User.java (User entity)
public class User
{
protected String firstName;
protected String lastName;
...
getters/setters (regular POJO)
}
UserSearchCommand
{
protected List<User> users;
protected int currentPage;
protected int sortColumnIndex;
protected SortOder sortOrder;
// the current user we're editing, if at all
protected User user;
public String getFirstName()
{return(user.getFirstName());}
public String getLastName()
{return(user.getLastName());}
}
Now, from my experience, this pattern or anti-pattern looks bad to me. For one, we're mixing several concerns together. While they're all user-related, it deviates from typical POJO design. If we're going to go this route, then shouldn't we do this instead?
UserSearchCommand
{
protected List<User> users;
protected int currentPage;
protected int sortColumnIndex;
protected SortOder sortOrder;
// the current user we're editing, if at all
protected User user;
public User getUser()
{return(user);}
}
Simply return the user object, and then we can call whatever methods on it as we wish?
Since this is quite different from typical bean development, JSR 303, bean validation doesn't work for this model and we have to write validators for every bean.
Does anyone else see anything wrong with this design pattern or am I just being picky as a developer?
Walter
© Stack Overflow or respective owner