import org.apache.commons.lang.StringUtils;
class User {
private String username;
private String password;
private String role;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setRole(String role) {
this.role = role;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getRole() {
return role;
}
}
abstract class Handler {
protected Handler next;
// 返回handler方便链式操作
public void next(Handler next) {
this.next = next;
}
// 流程开始的方法
public abstract void doHandler(User user);
static class Builder<T> {
private Handler head;
private Handler tail;
public Builder<T> addHandler(Handler handler) {
if (this.head == null) {
this.head = this.tail = handler;
System.out.println(this.head.next);
return this;
}
this.tail.next(handler);
this.tail = handler;
return this;
}
public Handler build() {
return this.head;
}
}
}
// 校验用户名或者密码是否为空
class ValidateHandler extends Handler {
@Override
public void doHandler(User user) {
if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
System.out.println("用户名或者密码为空!");
return;
}
System.out.println("校验通过");
next.doHandler(user);
}
}
// 登录校验,校验用户名是否匹配密码
class LoginHandler extends Handler {
@Override
public void doHandler(User user) {
if (!"pyy52hz".equals(user.getUsername()) || !"123456".equals(user.getPassword())) {
System.out.println("用户名或者密码不正确!请检查!");
return;
}
user.setRole("admin");
System.out.println("登陆成功!角色为管理员!");
next.doHandler(user);
}
}
// 权限校验
class AuthHandler extends Handler {
@Override
public void doHandler(User user) {
if (!"admin".equals(user.getRole())) {
System.out.println("无权限操作!");
return;
}
System.out.println("角色为管理员,可以进行下一步操作!");
}
}
// 登录流程
class LoginService {
public void login(User user) {
Handler.Builder builder = new Handler.Builder();
builder.addHandler(new ValidateHandler())
.addHandler(new LoginHandler())
.addHandler(new AuthHandler());
builder.build().doHandler(user);
}
}
public class Test{
// 测试方法
public static void main(String[] args){
User user = new User();
//校验通过
//用户名或者密码不正确!请检查!
user.setUsername("pyy52hz");
user.setPassword("1234567");
LoginService loginService = new LoginService();
loginService.login(user);
//校验通过
//登陆成功!角色为管理员!
//角色为管理员,可以进行下一步操作!
user.setUsername("pyy52hz");
user.setPassword("123456");
loginService.login(user);
}
}
class User {
private String username;
private String password;
private String role;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setRole(String role) {
this.role = role;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getRole() {
return role;
}
}
abstract class Handler {
protected Handler next;
// 返回handler方便链式操作
public void next(Handler next) {
this.next = next;
}
// 流程开始的方法
public abstract void doHandler(User user);
static class Builder<T> {
private Handler head;
private Handler tail;
public Builder<T> addHandler(Handler handler) {
if (this.head == null) {
this.head = this.tail = handler;
System.out.println(this.head.next);
return this;
}
this.tail.next(handler);
this.tail = handler;
return this;
}
public Handler build() {
return this.head;
}
}
}
// 校验用户名或者密码是否为空
class ValidateHandler extends Handler {
@Override
public void doHandler(User user) {
if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
System.out.println("用户名或者密码为空!");
return;
}
System.out.println("校验通过");
next.doHandler(user);
}
}
// 登录校验,校验用户名是否匹配密码
class LoginHandler extends Handler {
@Override
public void doHandler(User user) {
if (!"pyy52hz".equals(user.getUsername()) || !"123456".equals(user.getPassword())) {
System.out.println("用户名或者密码不正确!请检查!");
return;
}
user.setRole("admin");
System.out.println("登陆成功!角色为管理员!");
next.doHandler(user);
}
}
// 权限校验
class AuthHandler extends Handler {
@Override
public void doHandler(User user) {
if (!"admin".equals(user.getRole())) {
System.out.println("无权限操作!");
return;
}
System.out.println("角色为管理员,可以进行下一步操作!");
}
}
// 登录流程
class LoginService {
public void login(User user) {
Handler.Builder builder = new Handler.Builder();
builder.addHandler(new ValidateHandler())
.addHandler(new LoginHandler())
.addHandler(new AuthHandler());
builder.build().doHandler(user);
}
}
public class Test{
// 测试方法
public static void main(String[] args){
User user = new User();
//校验通过
//用户名或者密码不正确!请检查!
user.setUsername("pyy52hz");
user.setPassword("1234567");
LoginService loginService = new LoginService();
loginService.login(user);
//校验通过
//登陆成功!角色为管理员!
//角色为管理员,可以进行下一步操作!
user.setUsername("pyy52hz");
user.setPassword("123456");
loginService.login(user);
}
}