[ThinkingInJava] 33. Dynamic proxy mechanism, thinkinginjava33
/*** Book: Thinking In Java * function: Dynamic proxy mechanism * file: SimpleProxyDemo. java * Time: April 15, 2015 21:41:39 * Author: cutter_point */package Lesson14TypeInformation; import static net. mindview. util. print. *; interface Interface {// one Interface provides two methods: void doSomething (); void somethingElse (String arg);} class RealObject implements interface {// implementation Interface @ Overridepublic void doSomething () {print ("doSomething") ;}@ Overridepublic void somethingElse (String arg) {print ("somthingElse" + arg );}} class SimpleProxy implements Interface {// after two interfaces are implemented, a variable private Interface proxied; public SimpleProxy (Interface proxied) {this. proxied = proxied;} public void doSomething () {print ("SimpleProxy doSomething"); // first implement your own functions, and then call the function proxied of the passed interface. doSomething ();} public void somethingElse (String arg) {print ("SimpleProxy somethingElse" + arg); proxied. somethingElse (arg) ;}} public class SimpleProxyDemo {public static void consumer (Interface iface) {iface. doSomething (); iface. somethingElse ("bonobo");} public static void main (String [] args) {consumer (new RealObject (); consumer (new SimpleProxy (new RealObject ())); // dynamic proxy }}
So I said, maybe my understanding is not deep enough. I think dynamic proxy is the combination of classes, and another class is used as the private variable in one class, to implement various functions, you can also execute the various functions of the proxy class, you can intercept various types, you can determine whether to execute the methods of this Member class, or execute
Output:
DoSomething obj1
SomthingElse bonobo obj1
SimpleProxy doSomething obj1
DoSomething obj1
SimpleProxy somethingElse bonobo obj1
SomthingElse bonobo obj1