"Onlookers" design mode (12)--The structure of the proxy mode (proxy pattern)

Source: Internet
Author: User

wikipedia


Proxy mode ( English:proxypattern) is a design pattern in programming .

The so-called proxy refers to a class that can be used as an interface to other things. An interface that a proxy can do anything: A network connection, a large object in memory, a file, or other resource that is expensive or cannot be copied. ----WIKIPEDIA


Personal Understanding

Proxy mode is to find another object as a proxy to implement the request for you, the proxy mode is divided into two types, one is the static proxy mode, the other is the dynamic proxy mode, the static proxy mode is the proxy class for it to create an object, the object that needs the proxy class is given to the object in the proxy class. Let the object in the proxy class replace the object of the class that needs the proxy to perform certain tasks. Dynamic proxy mode uses the dynamic agent of JDK, and generates proxy object based on its class loader and its implemented interface, and realizes dynamic proxy through reflection mechanism.


Static proxy

Assuming such a scenario, the file has an open interface, then open a PDF file, I can use the proxy mode to deal with such a scenario, then the design of a class diagram:

Through the static proxy mode, the Pdfproxy object acts as a proxy object for the Pdffile, and then performs the tasks entrusted to him.

The main code (here only the code of the proxy class, all code: Design pattern Code):

public class Pdfproxy implements file{private file File = null, @Overridepublic void OpenFile () {if (file = = null) {file = NE W Pdffile ();} File.OpenFile ();}}

when making a call in a test class:

Pdfproxy proxy = new Pdfproxy ();p roxy.openfile ();

The method of invoking an object that needs to be proxied is directly through the Pdfproxy object, which, of course, needs to implement the same interface, so that the methods in the proxy class are guaranteed to be consistent with the methods in the Proxied object when making the call.



Dynamic Agent


Using the JDK's dynamic proxy, here are a few parameters, the class loader, the interface, and the instance of the proxy instance that handles the invocation of the class.

The code for the proxy class is as follows:

public class Pdfproxy {public static object GetProxy (final Object obj) {return proxy.newproxyinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), new Invocationhandler () {@Overridepublic object Invoke (object proxy , method method, object[] args) throws Throwable {Method.invoke (obj, args); return null;}});}

The call is made directly from the class name in the test class, and the above method declares that obj is final because the external data needs to be set to the final type in the internal anonymous class. The premise of using the JDK dynamic agent is to determine that the class has an implementation interface, and there is no way to create a proxy object, and the proxy mode has cglib way to be created, he is using inheritance, for the target class to generate subclasses to cover the target method implementation enhancements.


At the same time, the proxy mode is covered in the previous blog post, you can refer to blog: Hibernate and proxy mode


design Pattern Source code

Design Pattern Source Code





"Onlookers" design mode (12)--structure of the proxy mode (type)

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.