I've answered this question on Stack Overflow. The problem is - there are 30 columns in an Excel sheet and I need to validate each of the fields with different validation logic. The code should avoid a lot of if-else blocks. I have used strategy pattern to solve this. I would like to get a review of this code.
interface IValidator<T> {
boolean validate(T field);
}
class SomeFieldOne<T> implements IValidator<T> {
public boolean validate(T field) {
System.out.println("SomeFieldOne validation");
return true; // return true/false based on validation
}
}
class SomeFieldTwo<T> implements IValidator<T> {
public boolean validate(T field) {
System.out.println("SomeFieldTwo validate");
return true; // return true/false based on validation
}
}
class Context {
private IValidator validator;
public Context(IValidator validator) {
this.validator = validator;
}
public boolean validate(String field) {
return this.validator.validate(field);
}
}
public class TestValidation {
public static void main(String[] args) {
Context context;
context = new Context(new SomeFieldOne());
System.out.println(context.validate("some field one"));
context = new Context(new SomeFieldTwo());
System.out.println(context.validate("some field two"));
// test other fields ....
// .........
}
}