Java proxy Mode)

Source: Internet
Author: User
Java proxy mode ()

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 three 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.

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 ();
//
The Request Method of the real object is executed here.





Postrequest ();
}





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 the dynamic proxy class of Java.
 
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): returns 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.

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:
Public class realsubject implements subject
{


Public
Realsubject ()


{}






Public
 
Void
Request ()


{
System. Out. println (
"
From real subject.
"
);
}




}


Proxy processor:

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;
}





Public
Object invoke (Object proxy, method, object [] ARGs)
Throws
Throwable


{
System. Out. println (
"
Before calling
"
 
+
Method );
Method. Invoke (sub, argS );

System. Out. println (
"
After calling
"
 
+
Method );

Return
 
Null
;
}




}



 

The internal attribute of the proxy class is the object class. In actual use, the class's constructor dynamicsubject (Object OBJ) is used to assign values to it. In addition, the invoke method is also implemented in this class.

Method. Invoke (sub, argS );

Actually, it is to call the method to be executed by the proxy object. The method parameter sub is the actual proxy object, and ARGs is the parameter required to execute the corresponding operation of the proxy object. Through the dynamic proxy class, we can execute some related operations before or after the call.

Client:

 

Import java. Lang. Reflect. invocationhandler;
Import java. Lang. Reflect. proxy;
Import java. Lang. Reflect. constructor;
Import java. Lang. Reflect. method;

Public class client
{


Static
 
Public
 
Void
Main (string [] ARGs)
Throws
Throwable


{

Realsubject rs
=
 
New
Realsubject ();
//
Specify the proxy class here



Invocationhandler DS
=
 
New
Dynamicsubject (RS );
Class CLS
=
Rs. getclass ();


//
The following is a one-time generation Proxy:



Subject subject
=
(Subject) proxy. newproxyinstance (CLS. getclassloader (), CLS. getinterfaces (), DS );
Subject. Request ();

}

 


Program running result:
Before calling public abstract void subject. Request ()
From real subject.
After calling public abstract void subject. Request ()

In this way, the realsubject object can be dynamically changed at runtime, and the subject interface to be controlled can be changed at runtime (dynamicsubject class) it can also be dynamically changed to achieve a Flexible Dynamic proxy relationship.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.