1. Dynamic Agent: Java in the Java.lang.reflect package has its own proxy support, using this package you can dynamically create a proxy class at run time, implement one or more interfaces, and forward the call of the method to the class you specified.
Protection agent: Determines whether a client can access an object's proxy based on access rights.
2. Mission: Target village's small partners to Blind Date ~ Please be responsible for the implementation of the dating service system.
(1) Personbean interface, allow to set or get information about a person, etc.
1 Public InterfacePersonbean {2 PublicString getName ();3 Public voidsetName (String name);4 PublicString Getgender ();5 Public voidSetgender (String gender);6 PublicString getinterests ();7 Public voidsetinterests (String interests);8 Public intgethotornotrating ();9 Public voidSethotornotrating (inthotornotrating); Ten}
View Code
(2) Instantiation of Personbean.
1 Public classPersonbeanimplImplementsPersonbean {2 String name;3 String gender;4 String interests;5 intRating//Total number6 intratingcount=0;//Number of ratings7 PublicString GetName () {8 returnname;9 }Ten Public voidsetName (String name) { One This. Name =name; A } - PublicString Getgender () { - returngender; the } - Public voidSetgender (String gender) { - This. Gender =gender; - } + PublicString getinterests () { - returninterests; + } A Public voidsetinterests (String interests) { at This. interests =interests; - } - Public intgetrating () { - returnrating; - } - Public voidSethotornotrating (intrating) { in This. rating + =rating; -ratingcount++; to } + Public intgethotornotrating () { - if(ratingcount==0){ the return0; *}Else{ $ return(rating/ratingcount);Panax Notoginseng } - } the}
View Code
So here's the problem: any client can call any method according to the way we define it. The return information for each method is public. But in our matchmaking system, we want users to be able to set their own information and prevent others from modifying it. And you can not give yourself a grade, only others score.
WORKAROUND: You must create two agents, one to access your own personbean, and one to access someone else's personbean, so that the agent can control which request is allowed in each case.
3. Dynamic Agent Creation
(1) Create Invocationhandler
Invocationhandler is called the processor, when the Proxy method SetName () is called, The Invocationhandler Invoke () method is called, and then handler to decide how to handle the request. Maybe it was sent to Realsubject. Because there are two agents, two Invocationhandler are created.
1 Importjava.lang.reflect.*;2 3 Public classOwnerinvocationhandlerImplementsInvocationhandler {4 Personbean Personbean;5 6 PublicOwnerinvocationhandler (Personbean personbean) {7 Super();8 This. Personbean =Personbean;9 }Ten One A Publicobject Invoke (Object proxy, Method method, object[] args) - throwsillegalaccessexception { - the Try { - //But you can't score for yourself . - if(Method.getname (). Equals ("Sethotornotrating")){ - Throw Newillegalaccessexception (); + } - Else if(Method.getname (). StartsWith ("Get")){ + //as long as it's get, yes . A returnMethod.invoke (Personbean, args); at } -}Catch(InvocationTargetException e) { - e.printstacktrace (); - } - - return NULL; in } - to}
View Code
1 Importjava.lang.reflect.*;2 3 Public classNonownerinvocationhandlerImplementsInvocationhandler {4 Personbean Personbean;5 6 PublicNonownerinvocationhandler (Personbean personbean) {7 Super();8 This. Personbean =Personbean;9 }Ten One A Publicobject Invoke (Object proxy, Method method, object[] args) - throwsillegalaccessexception { - the Try { - //get is available - if(Method.getname (). StartsWith ("Get")){ - returnMethod.invoke (Personbean, args); + } - //set can only be scored + Else if(Method.getname (). Equals ("Sethotornotrating")){ A returnMethod.invoke (Personbean, args); at } - Else if(Method.getname (). StartsWith ("Set")){ - //if the other set - Throw Newillegalaccessexception (); - } -}Catch(InvocationTargetException e) { in e.printstacktrace (); - } to + return NULL; - } the *}
View Code
(2) Write code to create dynamic proxy
(3) Use the appropriate agent to wrap any Personbean object.
To be Continued
Headfirst Learning notes: The three protection agents of proxy mode (5.2)