抽象处理者
/**
* @Author: chuan.bai
* @Description 抽象处理者
* @Date: Created on 15:45 2021/3/24
* @Modified By:
*/
public abstract class Handler {
protected Handler nextHandler ;
protected String stepName;
//需要说明位于哪一步
public Handler( String stepName) {
this.stepName = stepName;
}
public void setNextStep(Handler handler){
this.nextHandler=handler;
}
public final Boolean handlerRequest(CheckVerifyBo request) {
//判断是否是自己的处理级别
Boolean response = true;
System.out.println(">>>>>>>>>>>>>>>>>>>> 开始判断流程:"+ this.stepName+" 是否匹配<<<<<<<<<<<<<<<<<<<<");
if(request.getNextStep().equals(this.stepName)){
System.out.println(">>>>>>>>>>>>>>>>>>>> 流程:"+ this.stepName+"成功匹配,开始执行 <<<<<<<<<<<<<<<<<<<<");
response= this.check(request);
}else {
//不属于自己的级别,判断是否由下一个处理者
if(nextHandler != null) {
System.out.println (">>>>>>>>>>>>>>>>>>>> 流程:"+this.stepName+ "不匹配寻找下一级流程 "+this.nextHandler.stepName+" <<<<<<<<<<<<<<<<<<<<");
response = this.nextHandler.handlerRequest(request);
}else {
System.out.println("流程已结束...");
}
}
return response;
}
public abstract Boolean check(CheckVerifyBo checkVerifyBo);
校验的具体规则类
/**
* @Author: chuan.bai
* @Description 校验的具体规则类
* @Date: Created on 16:14 2021/3/24
* @Modified By:
*/
public class CheckVerifyBo {
private String username;
private String email;
private int status;
private String password;
private String nextStep;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNextStep() {
return nextStep;
}
public void setNextStep(String nextStep) {
this.nextStep = nextStep;
}
}
校验用户昵称
/**
* @Author: chuan.bai
* @Description 校验用户昵称
* @Date: Created on 16:25 2021/3/24
* @Modified By:
*/
public class CheckUserNameVerify extends Handler {
public CheckUserNameVerify() {
super("username");
}
@Override
public Boolean check(CheckVerifyBo checkVerifyBo) {
System.out.println("用户:"+checkVerifyBo.getUsername()+"开始校验");
System.out.println("start check username");
if (checkVerifyBo.getUsername().equals("admin")||checkVerifyBo.getUsername().equals("basil")) {
//admin 验证通过
System.out.println("username pass");
checkVerifyBo.setNextStep("email");
return this.nextHandler.handlerRequest(checkVerifyBo);
}else {
System.out.println("username refuse");
return false;
}
}
}
校验邮箱
/**
* @Author: chuan.bai
* @Description 校验邮箱
* @Date: Created on 16:31 2021/3/24
* @Modified By:
*/
public class CheckEmailVerify extends Handler{
public CheckEmailVerify() {
super("email");
}
@Override
public Boolean check(CheckVerifyBo checkVerifyBo) {
System.out.println("start check email");
if (checkVerifyBo.getEmail().equals("admin@email")) {
//admin 验证通过
System.out.println("email pass");
checkVerifyBo.setNextStep("status");
return this.nextHandler.handlerRequest(checkVerifyBo);
}else {
System.out.println("email Refuse");
return false;
}
}
}
校验状态
/**
* @Author: chuan.bai
* @Description 校验状态
* @Date: Created on 16:34 2021/3/24
* @Modified By:
*/
public class CheckStatusVerify extends Handler{
public CheckStatusVerify() {
super("status");
}
@Override
public Boolean check(CheckVerifyBo checkVerifyBo) {
System.out.println("start check status");
if (checkVerifyBo.getStatus()==1) {
//admin 验证通过
System.out.println("status pass");
checkVerifyBo.setNextStep("password");
return this.nextHandler.handlerRequest(checkVerifyBo);
}else {
System.out.println("status Refuse");
return false;
}
}
}
校验密码
/**
* @Author: chuan.bai
* @Description 校验密码
* @Date: Created on 16:31 2021/3/24
* @Modified By:
*/
public class CheckPasswordVerify extends Handler{
public CheckPasswordVerify() {
super("password");
}
@Override
public Boolean check(CheckVerifyBo checkVerifyBo) {
System.out.println("start check password");
if (checkVerifyBo.getPassword().equals("123456")) {
//admin 验证通过
System.out.println("password pass");
return true;
}else {
System.out.println("password Refuse");
return false;
}
}
}
校验规则客户端
/**
* @Author: chuan.bai
* @Description 校验规则客户端
* @Date: Created on 16:36 2021/3/24
* @Modified By:
*/
public class AuthClient {
public static void main(String[] args) {
CheckUserNameVerify checkUserNameVerify =new CheckUserNameVerify() ;
CheckEmailVerify checkEmailVerify = new CheckEmailVerify();
CheckStatusVerify checkStatusVerify = new CheckStatusVerify();
CheckPasswordVerify checkPasswordVerify = new CheckPasswordVerify();
checkUserNameVerify.setNextStep(checkEmailVerify);
checkEmailVerify.setNextStep(checkStatusVerify);
checkStatusVerify.setNextStep(checkPasswordVerify);
CheckVerifyBo checkVerifyBo = new CheckVerifyBo();
//用户admin信息赋值
checkVerifyBo.setEmail("admin@email");
checkVerifyBo.setPassword("123456");
checkVerifyBo.setStatus(1);
checkVerifyBo.setUsername("admin");
//开始进行校验
checkUserNameVerify.check(checkVerifyBo);
//用户basil信息赋值
checkVerifyBo.setEmail("admin@email");
checkVerifyBo.setPassword("344556");
checkVerifyBo.setStatus(1);
checkVerifyBo.setUsername("basil");
//开始进行校验
checkUserNameVerify.check(checkVerifyBo);
}
}
输出结果
用户:admin开始校验
start check username
username pass
>>>>>>>>>>>>>>>>>>>> 开始判断流程:email 是否匹配<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>> 流程:email成功匹配,开始执行 <<<<<<<<<<<<<<<<<<<<
start check email
email pass
>>>>>>>>>>>>>>>>>>>> 开始判断流程:status 是否匹配<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>> 流程:status成功匹配,开始执行 <<<<<<<<<<<<<<<<<<<<
start check status
status pass
>>>>>>>>>>>>>>>>>>>> 开始判断流程:password 是否匹配<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>> 流程:password成功匹配,开始执行 <<<<<<<<<<<<<<<<<<<<
start check password
password pass
用户:basil开始校验
start check username
username pass
>>>>>>>>>>>>>>>>>>>> 开始判断流程:email 是否匹配<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>> 流程:email成功匹配,开始执行 <<<<<<<<<<<<<<<<<<<<
start check email
email pass
>>>>>>>>>>>>>>>>>>>> 开始判断流程:status 是否匹配<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>> 流程:status成功匹配,开始执行 <<<<<<<<<<<<<<<<<<<<
start check status
status pass
>>>>>>>>>>>>>>>>>>>> 开始判断流程:password 是否匹配<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>> 流程:password成功匹配,开始执行 <<<<<<<<<<<<<<<<<<<<
start check password
password Refuse
Process finished with exit code 0
网友评论