Static proxy and dynamic proxy

Source: Internet
Author: User

Static proxy and dynamic proxy

 

I remember that I was just getting started with proxies. I was in the big talk design mode. Recently, I learned some java techniques and got started with static proxies and dynamic proxies, especially dynamic proxies. When I was learning AOP, dynamic proxy is used. I will use some examples to summarize static proxy and dynamic proxy.

In fact, the biggest advantage after using a proxy is to hide the real class (delegate class), which is more secure. The biggest difference between a static proxy and a dynamic proxy is that, the agent class of the static proxy is written by the programmer and exists before the program runs. The dynamic proxy is generated dynamically when the program runs, and because the dynamic proxy is more flexible, it is also often used.

First, use a UML diagram to understand what the proxy is like?

 

In fact, the static proxy is to put a reference to the delegate class in the proxy class, and then use the delegate class as the constructor parameter of the proxy class, so that the delegate class will not be seen during client calls.

1. Integrate the public interface, delegate class, and proxy class, so that the proxy class has all the methods and attributes of the delegate class.

 

******************** **/Public interface ITest {public void addStudent (String Name );}
2. Delegate class: interface implementation

 

 

******************** **/Public class Test implements ITest {/*** executes the task with the given name. Print the Student Name and sleep for a long time */@ Overridepublic void addStudent (String Name) {System. out. println (Student Name: + Name); try {Thread. sleep (500);} catch (InterruptedException e) {e. printStackTrace ();}}}
3. proxy class: shows the behavior of the delegate class

 

 

/*********************************/Public class ProxyTest implements ITest {// reference private Test delegate for the delegate class held by the proxy class; public ProxyTest (Test delegate) {this. delegate = delegate;}/*** assigns the request to the delegate class for execution, and records the time before and after the task execution. The time difference is the processing time of the task */@ Overridepublic void addStudent (String Name) {long stime = System. currentTimeMillis (); // assign the request to the delegate class to process the delegate. addStudent (Name); long ftime = System. currentTimeMillis (); System. out. println (execution time + (ftime-stime) + millisecond );}}
4. Proxy Factory

 

 

/******** Proxy factory ********/public class TestFactory {public static ITest getInstance () {// For the customer class, it does not know whether the returned result is a proxy object or a delegate Class Object return new ProxyTest (new Test ());}}

5. Client call

 

 

Public class Client {/*** @ param args */public static void main (String [] args) {ITest proxy = TestFactory. getInstance (); proxy. addStudent (good student );}}
What are the advantages and disadvantages of static proxy? Why is dynamic proxy generated?

 

Advantage: the business class only needs to focus on the business logic itself, ensuring the reusability of the business class. This is a common advantage of proxy.
Disadvantages:
1) An interface of a proxy object serves only one type of object. If there are many proxy methods, proxy is required for each method, static proxies cannot be competent when the program size is relatively large.
2) if a method is added to the interface, all the proxy classes also need to implement this method except all the implementation classes. Added the complexity of code maintenance.

The Implementation of Dynamic proxy is based on three java APIs. The following is a brief introduction:

1. java. lang. reflect. Proxy

This is the parent class of all dynamic proxy classes generated by the Java Dynamic proxy mechanism. It provides a set of static methods to dynamically generate proxy classes and their objects for a group of interfaces.

 

// Method 1: This method is used to obtain the call processor static InvocationHandler getInvocationHandler (Object proxy) associated with the specified proxy Object. // Method 2: this method is used to obtain the static Class getProxyClass (ClassLoader loader, Class [] interfaces) Class Object associated with the specified Class loader and a group of interfaces. // method 3: this method is used to determine whether the specified Class object is a dynamic proxy Class static boolean isProxyClass (Class cl) // Method 4: this method is used to generate a static Object newProxyInstance (ClassLoader loader, Class [] interfaces, InvocationHandler h) for the specified Class loader, a group of interfaces, and call the processor)
Of course, I don't think you need to remember these methods. You only need to check the APIS when using them, so it is easy to use them when using them ~~

 

2. java. lang. reflect. InvocationHandler

This is to call the processor interface. It customizes an invoke Method for centralized processing of method calls on a dynamic proxy object. Generally, this method implements proxy access to the delegate class. Each time a dynamic proxy class object is generated, a corresponding call processor object must be specified.

// This method is used to centrally process all method calls in the dynamic proxy class. The first parameter is a proxy instance, and the second parameter is the called method object // The third method is the call parameter. The call processor performs preprocessing based on these three parameters or distributes the request to the delegate class instance for reflection and execution of Object invoke (Object proxy, Method method, Object [] args)
3. java. lang. ClassLoader (when I saw the java class loader and suddenly thought of drp, I also mentioned the tomcat Class Loader)

 

This is a class loader class that loads the class bytecode into the Java Virtual Machine (JVM) and defines class objects for it before the class can be used. The dynamic Proxy class generated by the Proxy static method also needs to be loaded by the class loader for use, the only difference between it and common classes is that its bytecode is dynamically generated by the JVM at runtime, rather than pre-stored in any one. class file.
Each time a dynamic proxy class object is generated, a Class Loader object must be specified.




 

 

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.