Design pattern series -- responsibility chain pattern

definition

In the responsibility chain mode, each node in the chain is regarded as an object, each node processes different requests, and the next node object is automatically maintained internally. When a request is sent from the first end of the chain, it will be passed to each node object in turn along the path of the chain until the request is processed by an object.

That is, multiple processing logical objects form a chain and pass the request along the chain until an object in the chain decides to process the request.

Implementation mode

To simulate the process of user login, first the system needs to verify whether the input parameters are legal, then verify whether the user exists, and finally verify the authority.

This case combines the idea of builder mode for demonstration. Readers who are not familiar with builder mode can go to Design pattern series -- builder pattern be familiar with

User login user

public class User {

	private String name;
	private String password;
	private String roleName;


	public User() {
	}

	public User(String name, String password, String roleName) {
		this.name = name;
		this.password = password;
		this.roleName = roleName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

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


	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	[@Override](https://my.oschina.net/u/1162528)
	public String toString() {
		return "User{" +
				"name='" + name + '\'' +
				", password='" + password + '\'' +
				", roleName='" + roleName + '\'' +
				'}';
	}
}

Handler login validation abstract object and inner class construction chain

public abstract class Handler<T> {
	protected Handler<T> next;

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

	public abstract void doHandler(User user);

	public static class Builder<T> {
		private Handler<T> head;
		private Handler<T> tail;

		public Builder<T> addHandler(Handler handler) {
			if (this.head == null) {
				this.head = handler;
				this.tail = handler;
			} else {
				this.tail.next(handler);
				this.tail = handler;
			}
			return this;
		}

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

ValidateHandler illegal validation processor

public class ValidateHandler extends Handler {
	[@Override](https://my.oschina.net/u/1162528)
	public void doHandler(User user) {
		String name = user.getName();
		String password = user.getPassword();
		if (name == null) {
			System.out.println("ValidateHandler:" + "User name cannot be empty");
			return;
		} else if (password == null) {
			System.out.println("ValidateHandler:" + "User password cannot be empty");
			return;
		}
		System.out.println("Passed ValidateHandler authentication");
		next.doHandler(user);
	}

}

LoginHandler login verification processor

import java.util.HashMap;
import java.util.Map;

public class LoginHandler extends Handler {

	private Map<String, String> userMap;

	public LoginHandler() {
		this.userMap = new HashMap<>();
		this.userMap.put("Zhang San", "zs123456");
		this.userMap.put("Li Si", "ls123456");
	}

	[@Override](https://my.oschina.net/u/1162528)
	public void doHandler(User user) {
		String name = user.getName();
		String password = user.getPassword();
		if (!userMap.keySet().contains(name)) {
			System.out.println("LoginHandler>>>>>>" + "Login user:"+name + "-----user does not exist");
			return;
		} else if (!userMap.values().contains(password)) {
			System.out.println("LoginHandler>>>>>>" + "Login user:"+ name  + "-----Incorrect password");
			return;
		}
		System.out.println("Passed LoginHandler authentication");
		next.doHandler(user);
	}
}

AuthHandler permission validation processor

import java.util.ArrayList;
import java.util.List;

public class AuthHandler extends Handler {

	private List<String> roleNameList;

	public AuthHandler() {
		this.roleNameList = new ArrayList<>();
		this.roleNameList.add("administrators");
		this.roleNameList.add("Ordinary users");
		this.roleNameList.add("tourist");
	}

	[@Override](https://my.oschina.net/u/1162528)
	public void doHandler(User user) {
		String roleName = user.getRoleName();
		if (!roleNameList.contains(roleName)) {
			System.out.println("AuthHandler:" + "The user role does not exist");
			return;
		} else if (!roleName.equals("administrators")) {
			System.out.println("AuthHandler:" + "Insufficient authority");
			return;
		}
		System.out.println("Passed AuthHandler authentication");
		System.out.println(user+">>>>>>>>>Login succeeded");

	}
}

LoginChainService login service

public class LoginChainService {

	public void doLogin(String userName, String password, String roleName) {
		Handler.Builder builder = new Handler.Builder();
		builder.addHandler(new ValidateHandler())
				.addHandler(new LoginHandler())
				.addHandler(new AuthHandler());
		builder.build().doHandler(new User(userName,password,roleName));
	}

Test test

public class Test {

	public static void main(String[] args) {
		LoginChainService loginChainService = new LoginChainService();
		loginChainService.doLogin("Zhang San","zs123456","administrators");
	}
}

}

Tags: Programming Java

Posted on Sun, 24 May 2020 09:03:34 -0700 by mikeglaz