1. Proxy Mode
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. (See document 1)
The following example uses Java and mode:
Abstract role:
Abstract Public class subject
{
Abstract Public void request ();
}
Real role: implements the subject request () method.
Public class realsubject extends subject
{
Public realsubject ()
{
}
Public void request ()
{
System. Out. println ("from real subject .");
}
}
Proxy role:
Public class proxysubject extends subject
{
Private realsubject; // attributes of a real role as a proxy role
Public proxysubject ()
{
}
Public void request () // This method encapsulates the Request Method of the real object
{
Prerequest ();
If (realsubject = NULL)
{
Realsubject = new realsubject ();
}
Realsubject. Request ();
// Execute the request method of the real object here
Postrequest ();
}
Private void prerequest ()
{
// Something you want to do before requesting
}
Private void postrequest ()
{
// Something you want to do after requesting
}
}
Client call:
Subject sub = new proxysubject ();
Sub. Request ();
From the above code, we can see 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, other methods (prerequest () and postrequest () are encapsulated to handle 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 Java
To solve the problem.
2. 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: this interface defines only one method 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 the internal H.
Static class getproxyclass (classloader loader, class [] interfaces): obtains 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 a proxy class (the method declared in the subject interface of 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, and it will not do substantial work for you. When generating its instance, you must provide a handler to take over the actual work. (See document 3)
When using a dynamic proxy class, we must implement the invocationhandler interface. Take the example in Section 1 as an example:
Abstract role (previously an abstract class, which should be changed to an interface here ):
Public interface subject
{
Abstract Public void request ();
}
Specific role realsubject: Same as above;
Proxy role:
Import java. Lang. Reflect. method;
Import java. Lang. Reflect. invocationhandler;
Public class dynamicsubject implements invocationhandler {
Private object sub;
Public dynamicsubject (){
}
Public dynamicsubject (Object OBJ ){
Sub = OBJ;
}