代理模式中的三个角色:
1. 声明真实对象和代理对象共同操作的接口
2. 真实对象(被代理对象)
3. 代理对象(包含被代理对象的一个对象引用)
静态代理模式:
//公共接口
interface SellInterface{
public void sell();
}
//真实类
class RadWineFactory implements SellInterface{
public void sell(){
System.out.println("红酒工厂:我是真实对象");
}
}
//代理类
class ProxyFactory implements SellInterface{
SellInterface realObj;
public ProxyFactory(SellInterface realObj){
this.realObj=realObj;
}
public void sell(){
System.out.println("工厂代理:我是代理对象,我先做点预备工作");
realObj.sell();
System.out.println("工厂代理:我是代理对象,我再做点后续工作");
}
}
//测试代码
public class SProxy{
public static void main(String[] args){
SellInterface obj=new ProxyFactory(new RadWineFactory());
obj.sell();
}
}
动态代理模式:
动态代理:顾名思义,就是不知道到底那个类实现的哪个接口需要做代理,在使用的时候,根据情况临时决定,代理类可以代理多个真实类。
java动态代理主要是使用java.lang.reflect包中的两个类。
在上面的静态代理中,我们强迫代理类RedWineProxy实现了抽象接口SellInterface.这导致我们的代理类无法通用于其他接口,所以不得不为每一个接口实现一个代理类.幸好,java为代理模式提供了支持。java主要是通过Proxy类和InvocationHandler接口来给实现对代理模式的支持的。
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.lang.reflect.Method;
//公共接口
interface SellInterface {
public void sell();
}
interface DisPlayInterface {
public void display();
}
// 真实类1:红酒厂商
class RadWineFactory implements SellInterface,DisPlayInterface {
public void display() {
System.out.println("红酒厂商.display()");
}
public void sell() {
System.out.println("红酒厂商.sell()");
}
}
//真实类2:米酒厂商
class RiceWineFactory implements SellInterface,DisPlayInterface{
public void display(){
System.out.println("米酒厂商.display()");
}
public void sell() {
System.out.println("米酒厂商.sell()");
}
}
// 代理类的调用处理程序
class ProxyObject implements InvocationHandler {
private Object realObj;
ProxyObject(Object realObj) {
this.realObj = realObj;
}
public static Object factory(Object realObj) {
Class cls = realObj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(),cls.getInterfaces(), new ProxyObject(realObj));
} //这段代码思路不太容易让人理解所谓动态代理,每个代理类的调用处理程序最少包含带有参数的构造方法和invoke方法即可。
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object mo = method.invoke(realObj, args);
return mo;
}
}
// 测试类
public class DProxy {
public static void main(String[] args) {
//使用RadWineFactory的SellInterface接口:
SellInterface radWineSellObj=(SellInterface)ProxyObject.factory(new RadWineFactory());
radWineSellObj.sell();
//使用RadWineFactory的DisPlayInterface 接口:
DisPlayInterface radWineDisObj = (DisPlayInterface) ProxyObject.factory(new RadWineFactory());
radWineDisObj.display();
//使用RadWineFactory的SellInterface接口:
SellInterface riceWineSellObj=(SellInterface)ProxyObject.factory(new RiceWineFactory());
riceWineSellObj.sell();
//使用RadWineFactory的DisPlayInterface 接口:
DisPlayInterface riceWineDisObj = (DisPlayInterface) ProxyObject.factory(new RiceWineFactory());
riceWineDisObj.display();
}
}
关于动态代理,AOP拦截器,现在看得不太懂,有几个参考链接如下:
http://www.iteye.com/topic/113089
http://www.iteye.com/topic/517835
http://rainsilence.iteye.com/blog/684265
http://www.iteye.com/topic/191339
http://www.iteye.com/topic/729903
http://www.iteye.com/topic/191339
多多思考,多多消化,嗯。
代理模式和装饰器模式的区别:
1. 目的不同。http://shipmaster.iteye.com/blog/675715
2. 代码结构稍有不同,装饰器模式强调必须实现装饰基类,装饰基类只是将被装饰类进行简单封装,并增添其他功能,而装饰基类的派生类可以进行多样化的装饰。例如下面的文字说明了该问题: