Significance
The invocation of an interface is unified into a function, and then a specific instance is called to invoke the appropriate method, acting as a distributor
Method handler, user--
Dictionary
Dynamic: Refers to creating a proxy class when the program is running
Proxy: The same property as the proxy class, that is, the proxy class has the Run method, it also has the Run method
Protection agent: Restricts some methods of an interface class to use for a particular object
Proxy class: Java Reflection package comes with, where newproxyinstance can return an instance of an interface implementation class
Composition
1. Interface
Person class: which has the name associated with the score method
I can change my name, but I can't change my score.
Other people can change their scores and not change their names.
Public Interface Personbean { String getName (); void setName (String name); int Getscore (); void setscore (int score);}
2. Classes that implement interfaces
Public classPersonbeanimplImplementsPersonbean {PrivateString name; Private intscore; PublicPersonbeanimpl () {} PublicPersonbeanimpl (String name,intscore) { This. Name =name; This. score =score; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetscore () {returnscore; } Public voidSetScore (intscore) { This. score =score; }}
3. Realizing the handler class of Invocationhandler
Its Invoke method uses reflection, which invokes invoke when the Personbean method is called, so that the corresponding logic can be implemented in the Invoke method body.
In the instance, some methods are qualified to allow calls, and some methods throw exceptions
3.1 Self Handler: When creating this handler instance, it means that only the name cannot be modified score
Public classOwninvocationhandlerImplementsInvocationhandler {//objects that require a proxy PrivatePersonbean Personbean; Owninvocationhandler (Personbean personbean) { This. Personbean =Personbean; } //Proxy Settings Rules PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable{String methodName=Method.getname (); //You can't set your own score. if("SetScore". Equals (MethodName)) { Throw NewIllegalaccessexception ("I Can't Score Myself"); } Else { returnMethod.invoke (Personbean, args); } }}
3.2 Others handler: When you create this handler instance, it means that you can only modify score cannot modify the name
Public classOtherinvocationhandlerImplementsInvocationhandler {//objects that require a proxy PrivatePersonbean Personbean; Otherinvocationhandler (Personbean personbean) { This. Personbean =Personbean; } //Proxy Settings Rules PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable{String methodName=Method.getname (); //can't change someone's name if("SetName". Equals (MethodName)) { Throw NewIllegalaccessexception ("Can't change someone's name"); } Else { returnMethod.invoke (Personbean, args); } }}
4. Production plant of Handler class
Get Personbean object through Proxy's newproxyinstance
Its main function is to bind the incoming Personbean with the specified Invocationhandler, and when Personbean invokes the method, Invocationhandler calls the Invoke method
Public classProxyfactory {/*** Get a personal agent *@paramPersonbean *@return */ PublicPersonbean getownerproxy (Personbean personbean) {return(Personbean) proxy.newproxyinstance (Personbean.getclass (). getClassLoader (), Personbean.getclass (). getIn Terfaces (),NewOwninvocationhandler (Personbean)); } /*** Get someone else's agent *@paramPersonbean *@return */ PublicPersonbean getotherproxy (Personbean personbean) {return(Personbean) proxy.newproxyinstance (Personbean.getclass (). getClassLoader (), Personbean.getclass (). getIn Terfaces (),NewOtherinvocationhandler (Personbean)); }}
Refer to "Head First design mode"--P474
Java Dynamic Agent mode--protection agent