本文说明:在学习《Java EE互联网轻量级框架整合开发》此书时,里面提到了几种设计模式,我在学习这几种设计模式时写了笔记,放上来让大家共同学习,如果有错误,望指出。
本章由两部分组成:
1、基本概念+关键代码讲解
2、完整例子
责任链是有多个拦截器的动态代理(可先看“设计模式——动态代理”及“设计模式——拦截器”),比如一张请假申请单,得经过项目经理的审批,部门经理的审批,人事的审批。这就需要三个拦截器,而且这三个拦截器并非并列而是递进。比如请假申请单拿到部门经理手里的时候,它需要知道项目经理批了没,有没有少天数之类的。所以三个拦截器是一层包着一层。
责任链实现代码跟拦截器一样,只有测试代码不一样。想要一层包着一层,很简单,只需要将得到的代理类proxy当做目标类参数target传入下一个代理类即可。非常简单,实现如下:
package com.amiao.design_pattern.chain_of_responsibility;
public class TestMain {
public static void main(String[] args) {
HelloWorldC proxy1 = (HelloWorldC) JdkInterceptorProxy.bind(new HelloWorldCImp(), "com.amiao.design_pattern.chain_of_responsibility.Interceptor1");
HelloWorldC proxy2 = (HelloWorldC) JdkInterceptorProxy.bind(proxy1, "com.amiao.design_pattern.chain_of_responsibility.Interceptor2");
HelloWorldC proxy3 = (HelloWorldC) JdkInterceptorProxy.bind(proxy2, "com.amiao.design_pattern.chain_of_responsibility.Interceptor3");
proxy3.say();
}
}
//包括2个接口,6个类,目标接口,目标类,拦截器接口,3个拦截器类,JDK拦截器动态代理类,测试类
//目标接口
package com.amiao.design_pattern.chain_of_responsibility;
public interface HelloWorldC {
public void say();
}
//目标类
package com.amiao.design_pattern.chain_of_responsibility;
public class HelloWorldCImp implements HelloWorldC{
@Override
public void say() {
System.out.println("Hello World C");
}
}
//拦截器接口
package com.amiao.design_pattern.chain_of_responsibility;
import java.lang.reflect.Method;
public interface Interceptor {
public boolean before(Object proxy, Object target, Method method, Object[] args);
public void around(Object proxy, Object target, Method method, Object[] args);
public void after(Object proxy, Object target, Method method, Object[] args);
}
//拦截器1
package com.amiao.design_pattern.chain_of_responsibility;
import java.lang.reflect.Method;
public class Interceptor1 implements Interceptor{
@Override
public boolean before(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器1】的before方法");
return true;
}
@Override
public void around(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器1】的around方法");
}
@Override
public void after(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器1】的after方法");
}
}
//拦截器2
package com.amiao.design_pattern.chain_of_responsibility;
import java.lang.reflect.Method;
public class Interceptor2 implements Interceptor{
@Override
public boolean before(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器2】的before方法");
return true;
}
@Override
public void around(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器2】的around方法");
}
@Override
public void after(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器2】的after方法");
}
}
//拦截器3
package com.amiao.design_pattern.chain_of_responsibility;
import java.lang.reflect.Method;
public class Interceptor3 implements Interceptor{
@Override
public boolean before(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器3】的before方法");
return true;
}
@Override
public void around(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器3】的around方法");
}
@Override
public void after(Object proxy, Object target, Method method, Object[] args) {
System.out.println("【拦截器3】的after方法");
}
}
//JDK拦截器动态代理类
package com.amiao.design_pattern.chain_of_responsibility;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JdkInterceptorProxy implements InvocationHandler {
private Object target = null;
private String interceptorClass = null;
public JdkInterceptorProxy(Object target, String interceptorClass) {
this.target = target;
this.interceptorClass = interceptorClass;
}
public static Object bind(Object target, String interceptorClass) {
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new JdkInterceptorProxy(target, interceptorClass));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(interceptorClass == null) {
return method.invoke(target, args);
}
Object returnObj = null;
Interceptor interceptor = (Interceptor) Class.forName(interceptorClass).newInstance();
if(interceptor.before(proxy, target, method, args)) {
returnObj = method.invoke(target, args);
}else {
interceptor.around(proxy, target, method, args);
}
interceptor.after(proxy, target, method, args);
return returnObj;
}
}
//测试类
package com.amiao.design_pattern.chain_of_responsibility;
public class TestMain {
public static void main(String[] args) {
HelloWorldC proxy1 = (HelloWorldC) JdkInterceptorProxy.bind(new HelloWorldCImp(), "com.amiao.design_pattern.chain_of_responsibility.Interceptor1");
HelloWorldC proxy2 = (HelloWorldC) JdkInterceptorProxy.bind(proxy1, "com.amiao.design_pattern.chain_of_responsibility.Interceptor2");
HelloWorldC proxy3 = (HelloWorldC) JdkInterceptorProxy.bind(proxy2, "com.amiao.design_pattern.chain_of_responsibility.Interceptor3");
proxy3.say();
}
}
/*
* 运行结果
* 【拦截器3】的before方法
* 【拦截器2】的before方法
* 【拦截器1】的before方法
* Hello World C
* 【拦截器1】的after方法
* 【拦截器2】的after方法
* 【拦截器3】的after方法
* */