【Design Pattern】——15, Chain of Responsibility Pattern

(15) Chain of Responsibility Model

The Chain of Responsibility Pattern regards each node in the chain as an object, each node is responsible for processing different requests, and automatically maintains the next node object internally. Through the responsibility chain mode, decoupling is well realized, and the object nodes are responsible for their own responsibility.

1. Principles of Chain of Responsibility Pattern Design

Abstract Handler: Define a request processing method and maintain a reference to the next processing object node;

Concrete handler (ConcreateHandler): Process the request.

2. Simple example

In the business logic of login, user information needs to be verified, judged, and authenticated. By using the responsibility chain mode, each processor can cooperate with each other to complete the login business processing.

public abstract class Handler {
    
    
    protected Handler chain;
//    负责将当前获取对象置为全局对象
    public void next(Handler handler){
    
    
        this.chain = handler;
    }
//   自己的业务逻辑处理
    public abstract void doHandler(Member member);
}
public class ValidHandler extends Handler {
    
    
    @Override
    public void doHandler(Member member) {
    
    
        if(null == member.getUsername() || null ==member.getPassword()){
    
    
            System.out.println("用户名或密码为空!");
            return;
        }
        System.out.println("用户名密码校验成功!");
        chain.doHandler(member);
    }
}
public class LoginHandler extends Handler {
    
    
    @Override
    public void doHandler(Member member) {
    
    
        System.out.println("登录成功!");
        member.setInfo("管理员");
        chain.doHandler(member);
    }
}
public class AuthHandler extends Handler {
    
    
    @Override
    public void doHandler(Member member) {
    
    
        if(!"管理员".equals(member.getInfo())){
    
    
            System.out.println("您不是管理员,没有操作权限!");
            return;
        }
        System.out.println("欢迎管理员!");
    }
}
public class Member {
    
    
    private String username;
    private String password;
    private String info;

    public Member(String username, String password, String info) {
    
    
        this.username = username;
        this.password = password;
        this.info = info;
    }
    //getter,setter
}
public class MemberService {
    
    
    public void login(String username,String password){
    
    
        ValidHandler validHandler = new ValidHandler();
        LoginHandler loginHandler = new LoginHandler();
        AuthHandler authHandler = new AuthHandler();

//        责任链模式的业务处理顺序,分别指定各处理器的当前对象
        validHandler.next(loginHandler);
        loginHandler.next(authHandler);

        validHandler.doHandler(new Member(username,password,"管理员"));
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        MemberService memberService = new MemberService();
        memberService.login("lsk","123");
    }
}
image-20210314121201123

3. Evaluation of the Chain of Responsibility Model

The chain of responsibility mode can realize the decoupling of request and processing, and at the same time, it can be handled flexibly and can be dynamically expanded. A long chain of responsibility can also lead to longer processing times and complex systems.

Guess you like

Origin blog.csdn.net/qq_40589204/article/details/118054636