java - design pattern for validations -
i developing project on spring+hibernate+annotations. need apply set of validations on data.
presently code seems this.
public class someclass{ boolean error = false; if(!error){ check condition1 if(fails) { error = true; } } if(!error){ check condition2 if(fails) { error = true; } } if(!error){ check condition3 if(fails) { error = true; } } // have 5 10 validations. }
is there design pattern can replace above scenario.
thanks.
spring offers validation classes, see org.springframework.validation
reference supplies full tutorial of way spring handles validation errors. http://static.springsource.org/spring/docs/2.5.x/reference/validation.html
on current project went bit further, crated validationtemplate class abstract. have 2 methods, validate method calls abstract method list<error>
when want validate can create anonymous instance of abstract class , implement doinvalidation method. allows to
new validationtemplate(){ doinvalidation(list<error> errors){ if(!condition) { errors.add(new error("reason"); } }.validate();
you can implement validation method want, throw exception or return list errors if want more elegant result.
unfortunately cannot post exact source instead of piece of pseudo code.
Comments
Post a Comment