关于后端:责任链模式

34次阅读

共计 2385 个字符,预计需要花费 6 分钟才能阅读完成。

通过构建一条流水线,将一个申请依照程序进行屡次解决

上面应用一个登录的例子来实现责任链模式

申请体类

public class UserInfo {
    private String username;
    private String password;
    private String role;

    public String getUsername() {return username;}

    public void setUsername(String username) {this.username = username;}

    public String getPassword() {return password;}

    public void setPassword(String password) {this.password = password;}

    public String getRole() {return role;}

    public void setRole(String role) {this.role = role;}
}

形象解决类

public abstract class Handler {
    Handler next;

    public void setNext(Handler handler){this.next = handler;}

    public abstract void handle(UserInfo info);
}

具体处理过程 验证明码 → 权限验证

public class LoginHandler extends Handler{
    @Override
    public void handle(UserInfo info) {if ("eacape".equals(info.getUsername()) && "123456".equals(info.getPassword())){System.out.println("明码验证通过");
            super.next.handle(info);
        }else {throw new RuntimeException("账户或明码谬误");
        }
    }
}
public class RoleHandler extends Handler{
    @Override
    public void handle(UserInfo info) {if ("1".equals(info.getRole())){System.out.println("登录胜利");
        }else {throw new RuntimeException("暂无权限");
        }
    }
}

测试

public class ChainTest {
    @Test
    public void test(){UserInfo info = new UserInfo();
        info.setUsername("eacape");
        info.setPassword("123456");
        info.setRole("1");
        Handler login = new LoginHandler();
        login.setNext(new RoleHandler());
        login.handle(info);
    }
}
====== 后果 ======
明码验证通过
登录胜利

能够与建造者模式联合用于构建调用链

形象解决类 & 结构者

public abstract class HandlerBuilder {
    HandlerBuilder next;

    public abstract void handle(UserInfo info);

    public static class  Builder{
        private HandlerBuilder tail;
        private HandlerBuilder head;

        public Builder addHandler(HandlerBuilder handler){if (head == null){head = tail = handler;}else {
                tail.next = handler;
                tail = tail.next;
            }
            return this;
        }

        public HandlerBuilder build(){return this.head;}
    }
}

具体处理过程

public class LoginHandler extends HandlerBuilder {
    @Override
    public void handle(UserInfo info) {if ("eacape".equals(info.getUsername()) && "123456".equals(info.getPassword())){System.out.println("明码验证通过");
            super.next.handle(info);
        }else {throw new RuntimeException("账户或明码谬误");
        }
    }
}
public class RoleHandler extends HandlerBuilder {
    @Override
    public void handle(UserInfo info) {if ("1".equals(info.getRole())){System.out.println("登录胜利");
        }else {throw new RuntimeException("暂无权限");
        }
    }
}

测试

public class ChainTest {
    @Test
    public void test(){UserInfo info = new UserInfo();
        info.setUsername("eacape");
        info.setPassword("123456");
        info.setRole("1");
        HandlerBuilder builder = new HandlerBuilder.Builder()
                .addHandler(new LoginHandler())
                .addHandler(new RoleHandler())
                .build();
        builder.handle(info);

    }
}
====== 后果 ======
明码验证通过
登录胜利

正文完
 0