Java中利用动态代理,生成“空”对象的例子


Java中利用动态代理,生成"空"对象的例子:

 
  1. package com.eric.reflect;  
  2.   
  3. import java.lang.reflect.InvocationHandler;  
  4. import java.lang.reflect.Method;  
  5. import java.lang.reflect.Proxy;  
  6. import java.util.Arrays;  
  7. import java.util.Collections;  
  8. import java.util.List;  
  9.   
  10. public class SnowRemovalRobot implements Robot {  
  11.     //generate a NULL robot object    
  12.     static Robot generateNullRobotObj(Robot obj) {  
  13.         return (Robot) Proxy.newProxyInstance(Robot.class.getClassLoader(), new Class[] {Robot.class,Null.class},  
  14.                 new NullRobotHandler(obj.getClass()));  
  15.     }  
  16.   
  17.     public static void main(String[] args) {  
  18.         Robot.Test.test(new SnowRemovalRobot("sample "));  
  19.         System.out.println();  
  20.         Robot.Test.test(generateNullRobotObj(new SnowRemovalRobot("sample ")));  
  21.     }  
  22.   
  23.     private String name;  
  24.   
  25.     public SnowRemovalRobot(String name) {  
  26.         this.name = name;  
  27.     }  
  28.   
  29.     public String name() {  
  30.         return name;  
  31.     }  
  32.   
  33.     public String model() {  
  34.         return "SnowBot Series 11";  
  35.     }  
  36.   
  37.     public List<Operation> operations() {  
  38.         return Arrays.asList(new Operation() {  
  39.             public String description() {  
  40.                 return name + " can shovel snow";  
  41.             }  
  42.   
  43.             public void command() {  
  44.                 System.out.println(name + " shoveling snow");  
  45.             }  
  46.         }, new Operation() {  
  47.             public String description() {  
  48.                 return name + " can chip ice";  
  49.             }  
  50.   
  51.             public void command() {  
  52.                 System.out.println(name + " chipping ice");  
  53.             }  
  54.         }, new Operation() {  
  55.             public String description() {  
  56.                 return name + " can clear the roof";  
  57.             }  
  58.   
  59.             public void command() {  
  60.                 System.out.println(name + " clearing roof");  
  61.             }  
  62.         });  
  63.     }  
  64.   
  65. }  
  66.   
  67. // null object handler, that use dynamic proxy to generate sepcial class null object   
  68. class NullRobotHandler implements InvocationHandler {  
  69.     private String name;  
  70.     private Robot nullRobot = new NullRobot();  
  71.   
  72.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  73.         return method.invoke(nullRobot, args);  
  74.     }  
  75.   
  76.     public NullRobotHandler(Class<? extends Robot> type) {  
  77.         name = "NULL " + type.getSimpleName() + " obj";  
  78.     }  
  79.   
  80.     class NullRobot implements Robot, Null {  
  81.   
  82.         public String name() {  
  83.             return name;  
  84.         }  
  85.   
  86.         public String model() {  
  87.             return name;  
  88.         }  
  89.   
  90.         public List<Operation> operations() {  
  91.             return Collections.emptyList();  
  92.         }  
  93.   
  94.     }  
  95.   
  96. }  
  97.   
  98. interface Robot {  
  99.     String name();  
  100.   
  101.     String model();  
  102.   
  103.     List<Operation> operations();  
  104.   
  105.     class Test {  
  106.         static void test(Robot robot) {  
  107.             System.out.println("name" + robot.name());  
  108.             System.out.println("model" + robot.model());  
  109.             for (Operation ope : robot.operations()) {  
  110.                 System.out.print("description:" + ope.description() + " command:");  
  111.                 ope.command();  
  112.             }  
  113.         }  
  114.     }  
  115.   
  116. }  
  117.   
  118. interface Operation {  
  119.     String description();  
  120.   
  121.     void command();  
  122. }  
  123.   
  124. /* 
  125.  *  
  126.  * History: 
  127.  *  
  128.  *  
  129.  *  
  130.  * $Log: $ 
  131.  */  

相关内容