歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java中利用動態代理,生成“空”對象的例子

Java中利用動態代理,生成“空”對象的例子

日期:2017/3/1 10:31:15   编辑:Linux編程

Java中利用動態代理,生成"空"對象的例子:

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

Copyright © Linux教程網 All Rights Reserved