一 静态代理实现!
1 继承:
简单,不做说明,
2 装饰者模式
其实就是对目标类的某个属性进行覆盖!上代码!
接口
public interface Service {
void said();
void sing();
}
实现类
public class ServiceImp implements Service {
@Override
public void said() {
System.out.println("讲故事");
}
@Override
public void sing() {
System.out.println("唱歌");
}
}
装饰者模式增强实现(简言之:就是覆盖,不好论述,看代码秒懂!更重要的是思路)
public class ServiceImpDecortar implements Service {
private Service service;
public ServiceImpDecortar(Service service) {
this.service = service;
}
@Override
public void said() {
service.sing();
System.out.println("大声的讲故事");
}
@Override
public void sing() {
service.said();
System.out.println("大声的唱歌");
}
}
测试
public class App {
public static void main(String[] args) {
//没有使用装饰者模式的情况下
Service service = new ServiceImp();
Service service1=new ServiceImpDecortar(service);
//增强后的方法
service1.said();
service1.sing();
}
}
二 动态代理实现
1 jdk动态代理增强(service接口和实现类不变,在词基础上增强)
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ServiceProxy {
public static Service createServiceProxy(Service service) {
ClassLoader classLoader = service.getClass().getClassLoader();
Class[] interfaces = service.getClass().getInterfaces();
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("said")) {
//执行原来的方法!(顺序和执行的逻辑有关)
Object invoke = method.invoke(service, args);
//做增强
System.out.println("大声的讲故事");
//返回方法的执行结果
return invoke;
} else if (method.getName().equals("sing")) {
//执行原来的方法!
Object invoke = method.invoke(service, args);
//做增强
System.out.println("大声的唱歌");
//返回方法的执行结果
return invoke;
}
//没有对应的方法
return null;
}
};
Service service1 = (Service) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
return service1;
}
}
测试:
public class App {
public static void main(String[] args) {
//没有使用装饰者模式的情况下
Service service = new ServiceImp();
// Service service1=new ServiceImpDecortar(service);
//jdk动态代理
Service service1= ServiceProxy.createServiceProxy(service);
//增强后的方法
service1.said();
service1.sing();
}
}
2 CGLIB动态代理(这种主要是基于Spring做的,导入Spring包即可,说到Spring在这提一下,Spring中通过动态代理加强的注入时机为运行时注入,灵活性比较的高,但是效率低,后面概述Spring在做聚体论述)!
CGLIB动态代理比较jdk的动态代理强大,可以对任意类做增强,不需要接口!
public class CGLIBService {
public static Service createService(Class clazz) {
//创建字节码对象
Enhancer enhancer = new Enhancer();
//设置Enhancer对象的父类是指定类型,也就是service的实现类即需要增强的类
enhancer.setSuperclass(clazz);
//设置回调方法
enhancer.setCallback(new MethodInterceptor() {
public Object intercept(Object o, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
//使用父类的方法调用
Object ret = methodProxy.invokeSuper(o, args);
if (method.getName().equals("said")) {
System.out.println("大声讲");
}
if (method.getName().equals("sing")) {
System.out.println("大声唱");
}
//返回执行结果
return ret;
}
});
//使用Enhancer对象创建对应的对象
return (Service) enhancer.create();
}
}
测试:
public class App {
public static void main(String[] args) {
//没有使用装饰者模式的情况下
// Service service = new ServiceImp();
// Service service1=new ServiceImpDecortar(service);
// Service service1= ServiceProxy.createServiceProxy(service);
Service service1 = CGLIBService.createService(ServiceImp.class);
//增强后的方法
service1.said();
service1.sing();
}
}
实际写代码时候用到来参考!