简体   繁体   English

用于验证文件的设计模式

[英]Design patterns for validating a file

We have to validate a CSV file containing various configuration parameters. 我们必须验证包含各种配置参数的CSV文件。 Are there any standard design patterns to do this type of validation. 是否有任何标准设计模式来进行此类验证。

More details: 更多细节:

  • There are different types of records - each with their own validation logic 有不同类型的记录 - 每个记录都有自己的验证逻辑
  • Certain records cross reference other records 某些记录交叉引用其他记录
  • There are rules on the order of the records 有关记录顺序的规则
  • There are rules on the eligibility of duplicate records 有重复记录资格的规则
  • etc 等等

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. 有一个抽象基类来表示Record,您可以使用Factory MethodSimple Factory来创建各种Record类型的具体实例。
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
    }

}

The template pattern may help: http://en.wikipedia.org/wiki/Template_method_pattern 模板模式可能有所帮助: http//en.wikipedia.org/wiki/Template_method_pattern

You set up a scaffolding for your validation in general terms, then hand off the algorithm to a delegate that knows how to handle specifics at various points. 您为一般术语的验证设置了一个脚手架,然后将算法交给一个知道如何在不同点处理细节的委托。

我知道我的一个朋友使用JBOSS DROOLS来验证这种文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM