The proxy mode provides a proxy for other objects to control access to this object. In some cases, a client does not want or cannot directly reference another object, and the proxy object can play a mediation role between the client and the target object.
The proxy mode generally involves the following roles:
Abstract role: Declares the common interfaces of real objects and proxy objects;
Proxy role: the proxy object role contains a reference to the real object to operate on the real object. At the same time, the proxy object provides the same interface as the real object to replace the real object at any time. At the same time, the proxy object can append other operations when performing real object operations, which is equivalent to encapsulating real objects.
Real role: the real object represented by the proxy role is the object we finally want to reference.
A simple example is as follows:
Package COM. java. proxy; interface subject {Abstract Public void request ();} class realsubject implements subject {@ overridepublic void request () {system. out. println ("real subject") ;}} class proxysubject implements subject {subject; Public proxysubject () {} public proxysubject (subject) {This. subject = subject;} @ overridepublic void request () {system. out. println ("preprocessing"); Subject. request (); system. out. println ("coming soon") ;}} public class proxymodel {public static void main (string [] ARGs) {subject = new proxysubject (New realsubject (); Subject. request ();}}
From aboveCodeIt can be seen that the customer actually needs to call the request () method of the realsubject class, And now uses proxysubject to proxy the realsubject class, which achieves the same purpose, at the same time, other methods can be encapsulated (some preprocessing and subsequent work can be done) to solve some other problems.
In addition, if you want to use the proxy mode according to the preceding method, the real role must exist beforehand and act as the internal attribute of the proxy object. However, in actual use, a real role must correspond to a proxy role. If a large number of roles are used, the class will expand sharply. In addition, if you do not know the real role, how can you use the proxy? This problem can be solved through the dynamic proxy class of Java.
Dynamic proxy
The Java Dynamic proxy class is located under the java. Lang. Reflect package and generally involves the following two classes:
(1 ).Interface invocationhandler: Only one method is defined in this interface.Object: invoke (Object OBJ, method, object [] ARGs). In actual use, the first parameter OBJ generally refers to the proxy class, and the method is the method to be proxy. In the preceding example, request () and ARGs are the parameter arrays of this method. This abstract method is dynamically implemented in the proxy class.
(2). Proxy: this class is a dynamic proxy class, which is similar to proxysubject in the previous example. It mainly includes the following content:
Protected proxy (invocationhandler H): Constructor, which is used to assign values to internal H values.
Static class getproxyclass (classloader loader, class [] interfaces): Get a proxy class. loader is the class loader, and interfaces is the array of all interfaces of the real class.
Static object newproxyinstance (classloader loader, class [] interfaces, invocationhandler H): Return an instance of the proxy class. The returned proxy class can be used as the proxy class (the method declared in the subject interface by the proxy class can be used ).
Dynamic proxy is such a class: it is a class generated at runtime. You must provide a set of interfaces to it when generating it, then the class declares that it implements these interfaces. Of course, you can use the class instance as any of these interfaces. Of course, this dynamic proxy is actually a proxy, it will not do substantive work for you, you must provide a handler when generating its instance, it takes over the actual work
Below we use dynamic proxy to rewrite the above example:
Package COM. java. proxy; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy; interface subject {Abstract Public void request ();} class realsubject implements subject {public realsubject () {}@ overridepublic void request () {system. out. println ("real subject") ;}} class proxysubject implements invocationhandler {private subject OBJ; Public proxysubject () {} P Ublic proxysubject (subject OBJ) {This. OBJ = OBJ ;}@ overridepublic object invoke (Object proxy, method, object [] ARGs) throws throwable {system. out. println ("preprocessing"); method. invoke (OBJ, argS); system. out. println ("coming soon"); return NULL ;}} public class proxymodel {public static void main (string [] ARGs) {realsubject = new realsubject (); Class <?> Linoleic = realsubject. getclass (); invocationhandler handler = new proxysubject (realsubject); Subject subject = (subject) proxy. newproxyinstance (fig. getclassloader (), fig. getinterfaces (), Handler); Subject. request ();}}
Finally, we provide an example of using a proxy in a remote method: Because the code is very long, it is not intendedArticlePaste the code, I uploaded the project to the micro disk, for: http://vdisk.weibo.com/s/jax8z
Project deployment: