1. 意图:
为其他对象提供一种代理以控制对这个对象的访问
2. 别名:
surrogate替身
3. 动机
按需创建, 替代对象
4. 适用性
* 远程代理
* 虚代理
* 保护代理
* 智能指引
5. 结构
 
6. 实例
    - package net.yeah.fanyamin.pattern.proxy;   
 
    -   
 
    -  
 
    -  
 
    -   
 
    - interface Greet {   
 
    -     void sayHello(String name);   
 
    -     void goodBye();   
 
    - }   
 
    -   
 
    - class GreetImpl implements Greet {   
 
    -     public void sayHello(String name) {   
 
    -         System.out.println("Hello " + name);   
 
    -     }   
 
    -     public void goodBye() {   
 
    -         System.out.println("Good bye.");   
 
    -     }   
 
    - }   
 
    -   
 
    - public class SimpleProxy implements Greet {   
 
    -     private Greet greet = null;   
 
    -        
 
    -     SimpleProxy(Greet greet) {   
 
    -         this.greet = greet;   
 
    -     }   
 
    -        
 
    -     public void sayHello(String name) {   
 
    -         System.out.println("--before method sayHello");   
 
    -         greet.sayHello(name);   
 
    -         System.out.println("--after method sayHello");   
 
    -     }   
 
    -        
 
    -     public void goodBye() {   
 
    -         System.out.println("--before method goodBye");   
 
    -         greet.goodBye();   
 
    -         System.out.println("--after method goodBye");   
 
    -     }   
 
    -      
 
    -  
 
    -   
 
    -     public static void main(String[] args) {   
 
    -         Greet greet = new SimpleProxy(new GreetImpl());   
 
    -         greet.sayHello("walter");   
 
    -         greet.goodBye();   
 
    -   
 
    -     }   
 
    -   
 
    - }  
 
 
package net.yeah.fanyamin.pattern.proxy;
/**
* @author walter
*/
interface Greet {
void sayHello(String name);
void goodBye();
}
class GreetImpl implements Greet {
public void sayHello(String name) {
System.out.println("Hello " + name);
}
public void goodBye() {
System.out.println("Good bye.");
}
}
public class SimpleProxy implements Greet {
private Greet greet = null;
SimpleProxy(Greet greet) {
this.greet = greet;
}
public void sayHello(String name) {
System.out.println("--before method sayHello");
greet.sayHello(name);
System.out.println("--after method sayHello");
}
public void goodBye() {
System.out.println("--before method goodBye");
greet.goodBye();
System.out.println("--after method goodBye");
}
/**
* @param args
*/
public static void main(String[] args) {
Greet greet = new SimpleProxy(new GreetImpl());
greet.sayHello("walter");
greet.goodBye();
}
}
 利用JDK中的动态代理
    -  
 
    -  
 
    -   
 
    - package net.yeah.fanyamin.pattern.proxy;   
 
    -   
 
    - import java.lang.reflect.InvocationTargetException;   
 
    - import java.lang.reflect.Method;   
 
    -   
 
    -  
 
    -  
 
    -   
 
    - public class DebugProxy implements java.lang.reflect.InvocationHandler {   
 
    -   
 
    -     private Object obj;   
 
    -   
 
    -     public static Object newInstance(Object obj) {   
 
    -         return java.lang.reflect.Proxy.newProxyInstance(obj.getClass().getClassLoader(),   
 
    -                                                         obj.getClass().getInterfaces(), new DebugProxy(obj));   
 
    -     }   
 
    -   
 
    -     private DebugProxy(Object obj) {   
 
    -         this.obj = obj;   
 
    -     }   
 
    -   
 
    -     public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {   
 
    -         Object result;   
 
    -         try {   
 
    -             System.out.println("--before method " + m.getName());   
 
    -             result = m.invoke(obj, args);   
 
    -         } catch (InvocationTargetException e) {   
 
    -             throw e.getTargetException();   
 
    -         } catch (Exception e) {   
 
    -             throw new RuntimeException("unexpected invocation exception: " + e.getMessage());   
 
    -         } finally {   
 
    -             System.out.println("--after method " + m.getName());   
 
    -         }   
 
    -         return result;   
 
    -     }   
 
    -   
 
    -      
 
    -  
 
    -   
 
    -     public static void main(String[] args) {   
 
    -         Greet greet = (Greet) DebugProxy.newInstance(new GreetImpl());   
 
    -         greet.sayHello("walter");   
 
    -         greet.goodBye();   
 
    -     }   
 
    -   
 
    - }  
 
动态代理确实很有价值,而且java的反射机制其实性能并不慢,只不过被代理的Object需要有个Interface就是了。 
实际中,代理多用在访问,权限控制 
其实从类的实现表现形式来说,和装饰模式,适配器模式,都比较相似,只不过具体实现意义不一样 
 
	posted on 2008-11-04 10:20 
caihaibo 阅读(113) 
评论(0)  编辑  收藏  所属分类: 
java模式