Without further ado, let’s begin today’s article

Definition of responsibility chain model:

Responsibility chain is a behavioral design mode: in general, responsibility chain is actually a chain of continuous processing units, from the head of the chain to the end of the chain to execute or jump out of the middle does not meet the conditions

The following will lead to the chain of responsibility model through login requirements, but it is not to say that the chain of responsibility model is better in this case

User login requirements:

1. Check whether the user exists 2. Check whether the user status is normal 3

Solution:

MemberDO memberDO = memberMapper.selectByEmail(email); / / 1. To determine whether a user exists ObjectUtil. IsNullToMessage (memberDO, ResultEnum. USER_NO_EXIT); //2. Check whether the user status is normal. Objects.equals(UserConstant.USER_STATUS_NORMAL,memberDO.getStatus())){ throw new ResultException(ResultEnum.USER_DISABLE); } //3. Check whether the password is correct. Objects.equals(memberDO.getPassword(),EncryptionUtil.EncoderPwdByMd5(loginDTO.getPassword(),null))){ throw new ResultException(ResultEnum.USER_PASSWORD_ERROR); }Copy the code

Chain of responsibility model solution

MemberDO memberDO = memberMapper.selectByEmail(email); loginDTO.setMemberDO(memberDO); AbstractHandler.Builder builder = new AbstractHandler.Builder(); Builder.addhandler (new UserHandler()) //2. Check the user status. AddHandler (new VerifyPermissionHandler()) //3. Check whether the password is correct. AddHandler (new AuthHandler()) // add the parameter.build().doHandler(loginDTO);Copy the code
public abstract class AbstractHandler<T> { protected AbstractHandler<T> next = null; Public void doHandler(loginDTO loginDTO); public void doHandler(loginDTO loginDTO); public void next(AbstractHandler handler) { this.next = handler; } public static class Builder<T> { private AbstractHandler<T> head; private AbstractHandler<T> tail; public Builder<T> addHandler(AbstractHandler handler) { if (this.head == null) { this.head = handler; this.tail = handler; } else { this.tail.next(handler); this.tail = handler; } return this; } public AbstractHandler build() { return this.head; }}}Copy the code

Concrete handler

public class UserHandler extends AbstractHandler { @Override public void doHandler(LoginDTO loginDTO) { MemberDO memberDO = loginDTO.getMemberDO(); / / to determine whether a user exists ObjectUtil. IsNullToMessage (memberDO, ResultEnum USER_NO_EXIT); if (next ! = null){ next.doHandler(loginDTO); } } } public class VerifyPermissionHandler extends AbstractHandler { @Override public void doHandler(LoginDTO loginDTO) { MemberDO memberDO = loginDTO.getMemberDO(); / / judge the user state if (Objects) equals (UserConstant. USER_STATUS_NORMAL, memberDO getStatus ())) {throw new ResultException(ResultEnum.USER_DISABLE); } if (next ! = null){ next.doHandler(loginDTO); } } } public class AuthHandler extends AbstractHandler { @Override public void doHandler(LoginDTO loginDTO) { MemberDO memberDO = loginDTO.getMemberDO(); // Check whether the password is correct. Objects.equals(memberDO.getPassword(),EncryptionUtil.EncoderPwdByMd5(loginDTO.getPassword(),null))){ throw new ResultException(ResultEnum.USER_PASSWORD_ERROR); } return; }}Copy the code

Advantages:

  1. Reduce coupling
  2. Adding new processing classes is convenient
  3. Allows dynamic modification of the order and number of processing classes

Disadvantages:

  1. The link length deteriorates the performance
  2. Easy to create a loop call