• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best Programming Practice  RSS feed

 
shown show
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some validator classes such as ABCValidator, PQRValidator, XYZValidator etc. All these classes contain only single static boolean method which takes single parameter and validate it.
For Example of ABCValidator class:

class ABCValidator {
public static boolean isValid(abc){
//abc validation logic
//return true/false
}
}

Is there any anti-pattern exists in above code ?
Rather than above approach, if we centralize all the methods in to one helper class, do we have any side effects ?
Is there any other approach to do in a better way ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shown show wrote:Is there any other approach to do in a better way ?

Well, one that springs to mind would be a typed Validator class or interface - ie, a Validator<T>.

The problem is that there are many ways to validate: range, pattern, or just a set of valid values spring to mind; and I suspect there are lots of others.

I think, if I was trying to implement something like this, I'd start out with a Validator interface that defines your isValid() method, and then subtype that for a specific "type" of check.

For example, a validation that involves a range could then be defined in a Bounds class, viz:and you could create other classes for other types (or styles) of validation.

Just one further thought: I tend not to like "is..." methods, because they violate the "tell, don't ask" principle.
Another way of doing it is with a valid() method, which can actually be built on top of your isValid() one, viz:giving your clients a choice of styles.

It could even be defined in an abstract Validator class, since it's not likely to change.

Like I say, it's just a personal preference, but it might be worth thinking about.

HIH

Winston

PS: Welcome to JavaRanch, shown.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!