(1):代理模式
代理模式:就是在原有功能之上提供額外的服務(wù)存皂。
特點:不改變目標(biāo)對象;擁有額外功能添谊。
分類:靜態(tài)代理痪伦、動態(tài)代理
動態(tài)代理:
1.實現(xiàn)invocationHandler接口
2.在實現(xiàn)接口的具體類當(dāng)中常見代理對象:Proxy.newProxyInstance()
3.操作代理對象的時候會執(zhí)行invoke()方法
public class LoginHandler implements InvocationHandler{
private Object delegate;//代理對象,就是userserviceIMP對象
public Object bind(Object delegate) {
this.delegate=delegate;
//創(chuàng)建代理對象:通過Proxy的靜態(tài)方法newProxyInstance()創(chuàng)建代理對象
return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(), //代理對象的接口就是代理接口userservice
this);
}
@Override
//對代理對象進(jìn)行操作的時候會執(zhí)行invoke方法
public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
// TODO Auto-generated method stub
Object result=null;
result=arg1.invoke(delegate, arg2);
return result;
}
}
public class Test {
public static void main(String[] args) {
LoggerHandler lh = new LoggerHandler();
UserService us = (UserService) lh.bind(new UserServiceImpl());//創(chuàng)建代理對象
us.login("adi", "123");
}
}
(2):aop簡介
AOP:面向切面編程
是一種思路遭贸,不是一種語言戈咳。典型的例子包括日志、驗證壕吹、事務(wù)管理等著蛙。