设计模式系列--责任链模式

定义

责任链模式是将链中每一个节点看作是一个对象,每个节点处理的请求均不同,且内部自动维护一个下一节点对象。当一个请求从链式的首端发出时,会沿着链的路径依次传递给每一个节点对象,直至有对象处理这个请求为止。

即 将多个处理逻辑对象形成一条链,并沿着这条链传递请求,直到链上的某一个对象决定处理此请求。

实现方式

模拟用户登陆过程,首先系统需要验证输入参数是否合法,然后验证用户是否存在,最后进行权限验证。

本案例结合了建造者模式思想进行演示,不熟悉建造者模式的读者可先前往 设计模式系列–建造者模式 熟悉

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
	public String toString() {
		return "User{" +
				"name='" + name + '\'' +
				", password='" + password + '\'' +
				", roleName='" + roleName + '\'' +
				'}';
	}
}

Handler 登陆验证抽象对象及内部类构造链

public abstract class Handler {
	protected Handler next;

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

	public abstract void doHandler(User user);

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

		public Builder 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 非法验证处理器

public class ValidateHandler extends Handler {
	@Override
	public void doHandler(User user) {
		String name = user.getName();
		String password = user.getPassword();
		if (name == null) {
			System.out.println("ValidateHandler:" + "用户姓名不能为空");
			return;
		} else if (password == null) {
			System.out.println("ValidateHandler:" + "用户密码不能为空");
			return;
		}
		System.out.println("已通过ValidateHandler认证");
		next.doHandler(user);
	}

}

LoginHandler 登陆验证处理器

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

public class LoginHandler extends Handler {

	private Map userMap;

	public LoginHandler() {
		this.userMap = new HashMap<>();
		this.userMap.put("张三", "zs123456");
		this.userMap.put("李四", "ls123456");
	}

	@Override
	public void doHandler(User user) {
		String name = user.getName();
		String password = user.getPassword();
		if (!userMap.keySet().contains(name)) {
			System.out.println("LoginHandler>>>>>>" + "登陆用户:"+name + "-----用户不存在");
			return;
		} else if (!userMap.values().contains(password)) {
			System.out.println("LoginHandler>>>>>>" + "登陆用户:"+ name  + "-----密码不正确");
			return;
		}
		System.out.println("已通过LoginHandler认证");
		next.doHandler(user);
	}
}

AuthHandler 权限验证处理器

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

public class AuthHandler extends Handler {

	private List roleNameList;

	public AuthHandler() {
		this.roleNameList = new ArrayList<>();
		this.roleNameList.add("管理员");
		this.roleNameList.add("普通用户");
		this.roleNameList.add("游客");
	}

	@Override
	public void doHandler(User user) {
		String roleName = user.getRoleName();
		if (!roleNameList.contains(roleName)) {
			System.out.println("AuthHandler:" + "该用户角色不存在");
			return;
		} else if (!roleName.equals("管理员")) {
			System.out.println("AuthHandler:" + "权限不足");
			return;
		}
		System.out.println("已通过AuthHandler认证");
		System.out.println(user+">>>>>>>>>登陆成功");

	}
}

LoginChainService 登陆服务

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 测试

public class Test {

	public static void main(String[] args) {
		LoginChainService loginChainService = new LoginChainService();
		loginChainService.doLogin("张三","zs123456","管理员");
	}
}

}

你可能感兴趣的:(设计模式)