/*** book: "Thinking In Java" * Features: Dynamic agent 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{//implements the interface @overridepublic void DoSomething () {print ("dosomething");} @Overridepublic void SomethingElse (String arg) {print ("Somthingelse" + arg);}} Class Simpleproxy implements interface{//implements two interfaces, a variable is added to the private Interface proxied;public Simpleproxy (Interface proxied) {this.proxied = proxied;} public void DoSomething () {print ("Simpleproxy dosomething");//implement own function first, then call the function of the interface passed in proxied.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}}
I just said, maybe my understanding is not deep enough, I think the dynamic agent is the use of the combination of classes, using a class inside the use of another class as a private variable, to achieve various functions, but also can execute the agent of the various functions of the class, can be various interception, you can determine whether or not to execute this member class method, Or do that.
Output:
DoSomething obj1
Somthingelse Bonobo obj1
Simpleproxy dosomething obj1
DoSomething obj1
Simpleproxy somethingelse Bonobo obj1
Somthingelse Bonobo obj1
"Thinkinginjava" 33, dynamic agent mechanism