1. 程式人生 > >利用責任鏈模式構建資料校驗模組

利用責任鏈模式構建資料校驗模組

這是一個比較簡單的表單校驗,利用責任鏈模式將多個數據校驗以“鏈”的形式串聯起來。
目前支援三種資料校驗(可拓展)
1.空資料校驗
2.資料格式校驗
3.非必填項校驗

程式碼:定義校驗器

public interface Verify {
    //校驗
    boolean doVerify();
    //設定下個校驗器
    void setNextVerify(Verify verify);
    //顯示錯誤資訊
    boolean showErrorMsg(String errorMsg);
    //執行校驗回撥(提供支援,專案未使用到)
    interface VerifyCallBack {
        //成功
        void sussecc();
        //失敗
        void failure();
    }
}

抽象校驗器

public abstract class AbsVerify implements Verify {

    protected String data;//校驗資料
    protected String errorMsg;//失敗資訊

    protected Verify nextVerify;   //下個執行的校驗器
    protected VerifyCallBack callBack;

    public AbsVerify(String data, String errorMsg) {
        this.data = data;
        this.errorMsg = errorMsg;
    }
    
    public void setNextVerify(Verify verify) {
        this.nextVerify = verify;
    }
    
    @Override
    public boolean showErrorMsg(String errorMsg) {
        if (callBack != null) {
            callBack.failure();
        }
        if (!isEmpty(errorMsg)) {
            System.out.println(errorMsg);
        }
        return false;
    }
    
    //執行下一個校驗
    protected boolean doNextFilter() {
        if (callBack != null) {
            callBack.sussecc();
        }
        return nextVerify != null ? nextVerify.doVerify() : true;
    }
    
    //是否為空
    protected boolean isEmpty(String msg) {
        return StringUtil.isBlank(msg);
    }
    
    public VerifyCallBack getCallBack() {
        return callBack;
    }
    
    public void setCallBack(VerifyCallBack callBack) {
        this.callBack = callBack;
    }
}

空資料校驗

public class EmptyVerify extends AbsVerify {

    public EmptyVerify(String data, String errorMsg) {
        super(data, errorMsg);
    }

    @Override
    public boolean doVerify() {
        return isEmpty(data) ? showErrorMsg(errorMsg) : doNextFilter();
    }
}

資料格式校驗(使用正則表示式)

public class RegularVerify extends AbsVerify {

    protected String regular;//正則表示式

    public RegularVerify(String data, String regular, String errorMsg) {
        super(data,errorMsg);
        this.regular = regular;
    }

    //是否允許為空
    protected boolean allowEmpty() {
        return false;
    }

    @Override
    public boolean doVerify() {
        if (isEmpty(data)) {
            return allowEmpty() ? doNextFilter() : showErrorMsg(errorMsg);
        }
        return matcher() ? doNextFilter() : showErrorMsg(errorMsg);
    }

    //正則表示式
    private boolean matcher() {
        Pattern p = Pattern.compile(regular);
        Matcher m = p.matcher(data);
        return m.matches();
    }
}

非必填項校驗

public class AllowEmptyVerify extends RegularVerify {

    public AllowEmptyVerify(String data, String regular, String errorMsg) {
        super(data, regular, errorMsg);
    }

    @Override
    protected boolean allowEmpty() {
        return true;
    }
}

校驗管理

public class VerifyManger {

    //校驗器集合
    private List<Verify> verifies = new ArrayList<>();

    //新增校驗器
    public VerifyManger addVerify (Verify verify) {
        verifies.add(verify);
        return this;
    }

    //執行校驗器
    public boolean execute() {
        if (verifies.isEmpty()) {
            return true;
        }
        int size = verifies.size();
        if (size == 1) {
            return verifies.get(0).doVerify();
        }
        for (int i = 0; i < size; i++) {
            Verify verify = verifies.get(i);
            if (i + 1 < size) {
                verify.setNextVerify(verifies.get(i + 1));
            }
        }
        return verifies.get(0).doVerify();
    }
}

正則表示式

public class Regular {

    //密碼(以字母開頭,長度在6~18之間,只能包含字母、數字和下劃線)
    public static final String PASSWORD = "^[a-zA-Z]\\w{6,18}$";
    //使用者名稱(中文、英文、數字包括下劃線)
    public static final String NAME = "^[\\u4E00-\\u9FA5A-Za-z0-9_]+$";
    //手機號
    public static final String PHONE = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";

}

使用案例場景

登入介面使用者需要填寫使用者名稱(必填)、密碼(必填)、手機號(非必填),點選登入進行校驗

public class Test {
    static String name = "童心未泯佐為";
    static String password = "qqwwee123";
    static String phone = "";

    public static void main(String args[]) {

        boolean execute = new VerifyManger()
                .addVerify(new EmptyVerify(name, "請填寫使用者名稱"))
                .addVerify(new RegularVerify(name, Regular.NAME, "使用者名稱格式不正確"))

                .addVerify(new EmptyVerify(password, "請填寫密碼"))
                .addVerify(new RegularVerify(password, Regular.PASSWORD, "密碼格式不正確"))

                .addVerify(new AllowEmptyVerify(phone, Regular.PHONE, "手機號格式不正確"))//非必填 允許為空 有值校驗
                .execute();
        if (execute) {
            System.out.println("校驗通過");
        }
    }
}

結果

校驗通過