We have to validate a CSV file containing various configuration parameters. Are there any standard design patterns to do this type of validation.
More details:
You can use the Strategy pattern to for validating the records. Have an abstract base class to represent a Record and you can use
Factory Method ,or Simple Factory to create concrete instances of various Record types.
Your specification is not complete. Here is the code sample that implements Strategy pattern
with a simplistic assumption about your record.
interface Validator { // since it is not clear what are the attributes that matter for a record, // this takes an instance of Record. // Modify to accept relevant attribures of Record public boolean validate (Record r); } class ConcreteValidator implements Validator { // implements a validation logic } // implements Comparable so that it can be used in rules that compare Records abstract class Record implements Comparable<Record> { protected Validator v; abstract void setValidator(Validator v); public boolean isValid() { return v.validate(this); } } class ConcreteRecord extends Record { // alternatively accept a Validaor during the construction itself // by providing a constructor that accepts a type of Validator // i.e. ConcreteRecord(Validator v) ... void setValidator(Validator v) { this.v = v; } // implementation of method from Comparable Interface public int compareTo(final Record o) {... } } public class Test { public static void main(String[] args) { // Store the read in Records in a List (allows duplicates) List<Record> recordList = new ArrayList<Record>(); // this is simplistic. Your Record creation mode might be // more complex, And you can use a Factory Method // (or Simple Factory) for creation of ConcreteRecord Record r = new ConcreteRecord(); r.setValidtor(new ConcretedValidator()); if (r.isValid()) { // store only valid records recordList.add(r); } // do further processing of Records stored in recordList } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With