设计模式之责任链

public abstract class AbsXXXValidator {
    /**
     * 下一个节点
     */
    protected AbsXXXValidator nextValidator;

    /**
     * set next.
     */
    public void setNextValidator(AbsXXXValidator absValidator) {
        this.nextValidator = absValidator;
    }

    /**
     * isValidatePass.
     *
     * @return true: pass
     */
    public abstract Result isValidatePass();

    /**
     * 执行下一个校验.
     *
     * @return 下一个校验是否通过
     */
    public Result nextValidator() {
        if (nextValidator == null) {
            return succeedResult();
        }
        return nextValidator.isValidatePass();
    }
}
public class XXXValidatorChainBuilder {
    private AbsXXXValidator firstValidator;

    private AbsXXXValidator lastValidator;

    /**
     * setValidator.
     *
     * @param absValidator {@link AbsXXXValidator}
     * @return 返会builder.
     */
    public XXXValidatorChainBuilder setValidator(AbsXXXValidator absValidator) {
        // 插入头结点
        if (firstValidator == null) {
            firstValidator = absValidator;
            lastValidator = absValidator;
            return this;
        }
        // 建立链表
        lastValidator.setNextValidator(absValidator);
        lastValidator = absValidator;

        return this;
    }

    /**
     * 获取链表的头结点
     *
     * @return {@link AbsXXXValidator}
     */
    public AbsXXXValidator build() {
        return firstValidator;
    }
}

你可能感兴趣的:(#,Java设计模式,设计模式,java,开发语言)