参考网址:http://www.cnblogs.com/micrari/p/5639084.html http://www.cnblogs.com/chinajava/p/5880870.html
一、代理模式
代理模式作用是在不改变被代理对象的内部代码结构时增加或者改善被代理对象的功能,分为静态代理和动态代理。
1.静态代理
代理对象和被代理对象实现同一个接口,代理对象需要持有被代理对象的引用,在调用被代理对象的方法之前或者之后切入新增的功能,从而实现增强被代理对象的功能的作用。实现代码如下:
a.要统一实现的接口
package com.heyuanjun.proxy;
public interface IProxy {
String save();
String delete();
}
b.被代理对象
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.heyuanjun.proxy;
public class Represented implements IProxy {
public Represented() {
}
public String save() {
System.out.println("执行保存操作");
return "成功";
}
public String delete() {
System.out.println("执行删除操作");
return "删除成功";
}
}
c.代理对象
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.heyuanjun.proxy;
public class MyProxy implements IProxy {
IProxy target; // 持有代理对象的引用
public MyProxy(IProxy target) {
this.target = target;
}
public String save() {
System.out.println("这是代理类添加的功能,事务开始"); //增强的功能
String returnValue = this.target.save();
System.out.println("这是代理类添加的功能,事务结束");
return returnValue;
}
public String delete() {
return null;
}
}
d. 测试
package com.heyuanjun.proxy;
public class TestProxy {
public TestProxy() {
}
public static void main(String[] args) {
IProxy iProxy = new MyProxy(new Represented()); // 传入被代理对象
iProxy.save(); // 调用方法,方法被增强了
}
}
静态代理优缺点:
具备代理模式的优点就是把业务 代码和非业务代理隔离出来,但是缺点也和明显,其一就是代理类和非代理类必须实现同样的接口,当接口有改动时代理类也需要相应的改动,如果项目中大量使用静态代理,无疑是给项目的维护增加了很大的难度。其二,静态代理需要给每个需要增强的方法编写 相应的代理方法,如此一来就增加了许多的代码量。
package com.heyuanjun.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactory {
Object target;
ProxyFactory(Object target) {
this.target = target;
}
public Object getProxyInstance() {
return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("动态代理前事务");
Object returnValue = method.invoke(ProxyFactory.this.target, args);
System.out.println("动态代理后事务");
return returnValue;
}
});
}
}
b、被代理对象
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.heyuanjun.proxy;
public class Represented implements IProxy {
public Represented() {
}
public String save() {
System.out.println("执行保存操作");
return "成功";
}
public String delete() {
System.out.println("执行删除操作");
return "删除成功";
}
}
c、测试
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.heyuanjun.proxy;
public class TestProxyFactory {
public TestProxyFactory() {
}
public static void main(String[] args) {
IProxy target = new Represented();
IProxy proxy = (IProxy)(new ProxyFactory(target)).getProxyInstance();
proxy.delete();
}
}
动态代理优缺点:
动态代理代理对象不需要和被代理对象实现同一个接口,不需要为被代理对象的每个方法添加代理方法,通过反射类Proxy和InvocationHandler回调接口实现的jdk动态代理,要求委托类必须实现一个接口,但事实上并不是所有类都有接口,对于没有实现接口的类,便无法使用该方方式实现动态代理。如果需要为没有实现接口的类实现动态代理,可以使用cglib动态代理。未完待续。。
各种IT书籍书目及下载链接
https://blog.csdn.net/dh1027/article/details/89327978