1
头图

通过构建一条流水线,将一个请求按照顺序进行多次处理

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

请求体类

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);

    }
}
======结果======
密码验证通过
登录成功

eacape
205 声望8 粉丝

JAVA 攻城狮


« 上一篇
命令模式
下一篇 »
Nginx入门笔记