模拟拦截器


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

//方式一 利用递归
public abstract class ActionInvocation {
private static List<Intercepter> intercepters = new ArrayList<Intercepter>();
private Iterator<Intercepter> iterator = null;
private static boolean bool = false;
public Object resultCode;

public Object invoke() throws Exception {
Intercepter intercepter = null;
// 利用递归,没吃都会执行invoke方法,所以用bool开关来是否第一次执行invoke,如果是则初始化iterator
if (bool == false) {
iterator = intercepters.iterator();
bool = true;
}
if (iterator.hasNext()) {
intercepter = (Intercepter) iterator.next();
resultCode = intercepter.intercept(this);
} else {
resultCode = execute();
// 还原bool,该功能主要是为了动态新增或删除intercepter
bool = false;
}
return resultCode;
}

abstract Object execute();

public boolean addIntercepter(Intercepter i) {
if (!intercepters.contains(i)) {
intercepters.add(i);
return true;
} else {
return false;
}
}

public Object remore(Intercepter i) {
return intercepters.remove(i);
}
}

class ActionInvocationImpl extends ActionInvocation {
Object execute() {
System.out.println("Hello World!");
return "Hello World!";
}
}

class Intercepter {
private int id;

public Intercepter(int id) {
this.id = id;
}

public Object intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("run the intercept()" + this.id);
Object resultCode = actionInvocation.invoke();
System.out.println("after the action run,run the intercept continue "
+ this.id);
return resultCode;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Intercepter other = (Intercepter) obj;
if (id != other.id)
return false;
return true;
}
}

class Test {
public static void main(String[] args) throws Exception {
ActionInvocation action = new ActionInvocationImpl();
action.addIntercepter(new Intercepter(1));
action.addIntercepter(new Intercepter(2));
action.addIntercepter(new Intercepter(3));
action.invoke();
// 可以动态新增和删除拦截器
action.remore(new Intercepter(2));
action.addIntercepter(new Intercepter(4));
action.invoke();
}
}

// 方式二 利用代理

//被代理接口
interface IHello {
public void say();
}
//被代理接口实现类
class HelloImpl implements IHello {

public void say() {
System.out.println("say Hello World!");
}
}

//拦截器接口
interface IntercepterA {
void before();

void after();
}
//日志拦截器类
class LogIntercepter implements IntercepterA {

public void before() {
System.out.println("开始");
}

public void after() {
System.out.println("结束");
}
}
//时间拦截器类
class TimeIntercepter implements IntercepterA {

long starttime;

public void after() {
System.out.println("结束时间为" + new Date());
System.out.println("方法运行时间为----"
+ (System.currentTimeMillis() - starttime));
}

public void before() {
System.out.println("开始时间为" + new Date());
this.starttime = System.currentTimeMillis();
}

}

//代理类
class MyProxy implements InvocationHandler {

private Object delegate;

private List<IntercepterA> intercepterList = new ArrayList<IntercepterA>();

public Object bind(Object ob) {
this.delegate = ob;
return Proxy.newProxyInstance(this.getClass().getClassLoader(), ob
.getClass().getInterfaces(), this);
}

public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
for (IntercepterA intercepterA : intercepterList) {
intercepterA.before();
}
result = method.invoke(delegate, args);
for (IntercepterA intercepterA : intercepterList) {
intercepterA.after();
}
return result;
}

public void addInterceper(IntercepterA i) {
this.intercepterList.add(i);
}

public void removeInterceper(IntercepterA i) {
this.intercepterList.remove(i);
}
}
//测试类
class TestA {
public static void main(String[] args) {
MyProxy proxy = new MyProxy();
proxy.addInterceper(new LogIntercepter());
proxy.addInterceper(new TimeIntercepter());
IHello hello = (IHello) proxy.bind(new HelloImpl());
hello.say();
}
}

// 两个方式各有优缺点,方式一速度快,但是只能拦截一个方法;方式二利用反射,速度相对慢点,但是可以可以针对所有方法拦截,但是第二个方式也有不好的地方拦截方法分成了before和after和能象第一种合成一体

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值