Understanding of the Java proxy pattern

Source: Internet
Author: User

The agent of Java is divided into static agent and dynamic agent.

Composition of the static proxy mode: 1. A common interface or abstract class 2. Real Class 3. proxy class

The real class and the proxy class all implement that common interface, the proxy class has a reference to the real class object, when the user uses the proxy class, it actually invokes the corresponding method of the real object.

The limitation of static agents is 1. There is a real class that constructs a proxy class, and the number of classes increases dramatically. 2. The proxy class cannot be written when writing code without determining the real class that needs to be proxied.


Dynamic Agent: It is to solve the defect of static agent. The way it's used is this:

The user needs to dynamically create a proxy class using the proxy class provided by Java, and use this proxy class to implement the corresponding function. For a proxy class, first it needs to know what class it wants to proxy, the arguments in the Newproxyinstance () method of the proxy class do not seem to provide such information, in fact, this information exists in its third parameter invocationhandler inside. Invocationhandler is responsible for the actual method invocation, there must be a member variable in the class that implements the Invocationhandler interface to store the instance of the real class, and the invocation of the real class method is implemented by the methods parameter in the Invoke method. The form of a shape like this:

@Overridepublic object Invoke (Object proxy, Method method, object[] args) throws throwable{System.out.println ("before CA  Lling: "+ method);    if (null! = args) {for (Object Obj:args) {System.out.println (obj);  }} Object object = Method.invoke (Proxyobj, args);  System.out.println ("After calling:" + method); return object;}

Here Invacationhandler is the real class and the bridge between the proxy class, the object of the proxy class in the dynamic generation only claims that they implement a certain set of interfaces, this set of interfaces with the real class implementation of the interface consistent, which is determined by the nature of the agent. The real class is encapsulated in the handler, and the handler invoke method uses the Java reflection mechanism to invoke the real class method. Dynamic proxies are highly flexible. First, we no longer intentionally create a proxy class for a class, the proxy class is dynamically generated, and the real class resolution can be dynamically obtained at runtime by handler when the real class is determined only at runtime.

This article is from the "Wood Leaf Thinking" blog, please make sure to keep this source http://muye12921.blog.51cto.com/5008231/1970245

Understanding of the Java proxy pattern

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.