Proxy mode, proxy mode java

Source: Internet
Author: User

Proxy mode, proxy mode java

The proxy mode provides a proxy for an object to control access to this object. The proxy class is responsible for preprocessing messages for the delegate class, filtering messages and forwarding messages, and subsequent processing after the message is executed by the delegate class. Through the proxy layer, direct access to delegate objects can be effectively controlled, and delegate objects can be well hidden and protected.

The proxy class is the same as the target class and cannot be changed. The proxy only controls the target, allowing you to access the proxy or not. However, the delegate class (business class) only needs to focus on the business logic itself, ensuring the reusability of the delegate class (business class.

Depending on the generation time of the proxy class, we can divide the proxy into static proxy and dynamic proxy.

(1) Static proxy

The so-called static proxy knows the relationship between the proxy class and the delegate class before running. It is created by a programmer or a tool to generate the source code of the proxy class and then compile the proxy class. The static proxy implementation code is as follows:

Proxy interface:

Package static_proxy_package;/*** proxy interface, send a gift * @ author lsh **/public interface Subject {/*** give a gift to someone * @ param gift */public void giveGift (String name );}
Delegate class:

Package static_proxy_package;/*** delegate class, the person who really wants to give gifts, implements the interface * @ author lsh **/public class RealSubject implements Subject {@ Overridepublic void giveGift (String name) {System. out. println (name + "gave you a gift ");}}

Proxy type:

Package static_proxy_package;/*** proxy class to help the principal send gifts, the proxy interface * @ author lsh **/public class ProxySubject implements Subject {// The proxy class holds a delegate class object to reference private Subject delegate; public ProxySubject (Subject delegate) {this. delegate = delegate;} @ Overridepublic void giveGift (String name) {System. out. println ("before giving gifts"); delegate. giveGift (name); System. out. println ("after gift ");}}

Client:

Package static_proxy_package; import java. lang. reflect. proxy;/*** Client class ** @ author lsh **/public class Client {public static void main (String [] args) {Subject realSubject = new RealSubject (); subject proxy = new ProxySubject (realSubject); proxy. giveGift ("huazi ");}}
With this static proxy, an interface of the proxy object serves only one type of object. If there are many proxy types, proxy is required for each type, static proxies cannot be competent when the program size is relatively large. In addition, you can think about it. If the interface needs to add a method to process all implementation classes, all proxy classes also need to implement this method to increase the complexity of the Code.

In this case, we can consider using dynamic proxy. The Proxy class is its parent class. This rule applies to all dynamic Proxy classes created by the Proxy. This class also implements a set of interfaces It proxies for. This is why it is safely converted to an interface it proxies, this saves unnecessary code generation and improves the efficiency of creating proxy classes. The figure is as follows:


In addition, the biggest benefit of dynamic proxy is that all methods declared in the interface are transferred to InvocationHandler. invoke for processing in a centralized method of the calling processor ). In this way, when there are a large number of interface methods, we can flexibly process them without the need to transfer each method like a static proxy. Next, let's take a look at the dynamic proxy.

(2) Dynamic Proxy:

The so-called dynamic proxy is the relationship between the proxy class and the delegate class is determined when the program is running. The source code of the dynamic proxy class is dynamically generated by the JVM according to the reflection mechanism during runtime, and there is no bytecode file of the proxy class. The following shows the dynamic proxy code implementation:

Proxy interface:

Package static_proxy_package;/*** proxy interface, send a gift * @ author lsh **/public interface Subject {/*** give a gift to someone * @ param gift */public void giveGift (String name );}

Delegate class:

Package static_proxy_package;/*** delegate class, the person who really wants to give gifts, implements the interface * @ author lsh **/public class RealSubject implements Subject {@ Overridepublic void giveGift (String name) {System. out. println (name + "gave you a gift ");}}

Proxy type:

Package static_proxy_package; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy;/*** use a dynamic Proxy to call the program processing class * @ author lsh **/public class proxyHandler implements InvocationHandler {// The Proxy class holds an Object of the delegate class to reference private Object delegate; public proxyHandler (Object delegate) {this. delegate = delegate;} public Subject getInstance () {return (Subject) Proxy. newProxyInstance (delegate. getClass (). getClassLoader (), delegate. getClass (). getInterfaces (), this) ;}@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {Object sub = null; System. out. println ("before giving gifts"); sub = method. invoke (delegate, args); System. out. println ("after gift"); return sub ;}}

Client:

Package static_proxy_package; import java. lang. reflect. proxy;/*** Client class ** @ author lsh **/public class Client {public static void main (String [] args) {// define a delegate object Subject delegate = new RealSubject (); // define a dynamic proxy class proxyHandler handler = new proxyHandler (delegate); Subject proxy = handler. getInstance (); proxy. giveGift ("huazi ");}}
This is a brief introduction to static and dynamic proxies. The following is a brief summary of static and dynamic proxies:



There are still many questions about the implementation of dynamic proxies in java. Next I will sort out and understand the implementation principles of dynamic proxies.

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.