簡體   English   中英

用於驗證文件的設計模式

[英]Design patterns for validating a file

我們必須驗證包含各種配置參數的CSV文件。 是否有任何標准設計模式來進行此類驗證。

更多細節:

  • 有不同類型的記錄 - 每個記錄都有自己的驗證邏輯
  • 某些記錄交叉引用其他記錄
  • 有關記錄順序的規則
  • 有重復記錄資格的規則
  • 等等

pattern to for validating the records. 您可以使用模式來驗證記錄。 ,or to create concrete instances of various Record types. 有一個抽象基類來表示Record,您可以使用來創建各種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
    }

}

模板模式可能有所幫助: http//en.wikipedia.org/wiki/Template_method_pattern

您為一般術語的驗證設置了一個腳手架,然后將算法交給一個知道如何在不同點處理細節的委托。

我知道我的一個朋友使用JBOSS DROOLS來驗證這種文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM