Java and mode Learning Series-proxy Mode

Source: Internet
Author: User

 

I, Proxy mode structure

The proxy mode provides a proxy object for an object and controls the reference to the original object. There are several categories based on the purpose of use:

Remote Proxy: provides a Local Area Representative object for an object in a different address space. This address space can be in this machine, but also in another machine. Remote proxy is also called ambassador.

Virtual Proxy: creates an object with high resource consumption as needed, so that the object is created only when necessary.

Copy-on-write Proxy: a virtual proxy. The replication (clone) operation is performed only when the client needs it.

Protection (protect or access) proxy: controls access to an object. If necessary, different users can be granted different levels of access permissions.

Cache Proxy: provides a temporary storage space for the results of a target operation so that multiple clients can share the results.

Firewall Proxy: protects the target from malicious users.

Synchronization Proxy: enables several users to simultaneously use an object without conflict.

Smart reference Proxy: provides some additional operations when an object is referenced, such as recording the number of calls to this object.

The structure is as follows:

II, Application of proxy mode in Java

JavaDynamic proxy

In the java. Lang. Reflect library, the following three classes support proxy mode: proxy, invocationhandler, and method. Follow these steps to create a dynamic proxy object:

1. Specify a series of interfaces to create a proxy object.

2. Create an invocation handler object.

3. Set the proxy to the proxy object of another object.

4. A proxy is used to call the invoke () method of the processor. On the one hand, the call is passed to the real object, and on the other hand, various operations need to be performed.

For example:

Import Java. AWT. list; <br/> Import Java. lang. reflect. invocationhandler; <br/> Import Java. lang. reflect. method; <br/> Import Java. lang. reflect. proxy; <br/> Import Java. util. vector; <br/> public class vectorproxy implements invocationhandler {</P> <p> private object proxyobj; <br/> Public vectorproxy (Object OBJ) {<br/> proxyobj = OBJ; <br/>}< br/> Public static object Factory (Object OBJ) {<br/> class CLS = OBJ. getclass (); <br/> return proxy. newproxyinstance (CLS. getclassloader (), CLS. getinterfaces (), new vectorproxy (OBJ); <br/>}< br/> Public object invoke (Object proxy, method, object [] ARGs) throws throwable {<br/> system. out. println ("before calling" + method); <br/> If (ARGs! = NULL) {<br/> for (INT I = 0; I <args. length; I ++) <br/> system. out. println (ARGs [I] + ""); <br/>}< br/> Object o = method. invoke (proxyobj, argS); <br/> system. out. println ("after calling" + method); <br/> return 0; <br/>}< br/> Public static void main (string [] ARGs) {<br/> List v = NULL; <br/> V = (list) Factory (new vector (10); <br/> v. add ("new"); <br/> v. add ("York"); <br/>}< br/>} 

III, Relationship between proxy mode and other modes

Adapter Mode

The adapter mode is similar to the proxy mode. They can be considered as an object that provides a front interface. The purpose of the adapter mode is to change the object interface to be considered, while the proxy mode does not change the interface of the object to be proxy.

Decoration Mode

The decoration mode has the same interface as the decorated object, so the two modes may be confused. However, the decoration mode should provide enhanced functions for the decorated object. The proxy mode controls the use of the object and does not provide enhanced functions for the object itself.

 

 

 

 

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.