A brief talk on the Java design mode--agent mode (proxy)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45568963
I. Overview

Provides a proxy for other objects to control access to this object.

Second, applicability

1. The remote Agent (REMOTEPROXY) provides a local representation of an object in different address spaces.

2. Virtual agents (Virtualproxy) create expensive objects as needed.

3. The protection agent (Protectionproxy) Controls access to the original object. 4. The Smart Guide (smartreference) replaces the simple pointer, which performs some additional operations when accessing an object.

Third, participants

1.Proxy saves a reference so that the agent can access the entity. If the interface of Realsubject and subject is the same, proxy references subject. Provides an interface that is the same as the subject interface, so that proxies can be used instead of entities. Controls access to the entity and may be responsible for creating and deleting it. Other features depend on the type of proxy:

2.RemoteProxy is responsible for encoding the request and its parameters and sending the encoded request to entities in different address spaces.

3.VirtualProxy can cache additional information for an entity in order to delay access to it.

4.ProtectionProxy checks whether the caller has the necessary access rights to implement a request.

5.Subject defines the common interface for realsubject and proxies, so that proxies can be used wherever realsubject is used.

6.RealSubject defines the entity represented by proxy

Four, class diagram

V. Examples

Proxy

Package com.lyz.design.proxy;/** * Proxy  * @author Liuyazhuang * */public class Proxyobject implements Object {    Ob Ject obj;        Public Proxyobject () {        System.out.println ("This is the proxy class");        obj = new Objectimpl ();    }        public void Action () {        System.out.println ("Agent Start");        Obj.action ();        SYSTEM.OUT.PRINTLN ("proxy End");}    }

Subject
Package com.lyz.design.proxy;/** * Subject  * @author Liuyazhuang * */public interface Object {    void action ();}

Realsubject

Package com.lyz.design.proxy;/** * Realsubject  * @author Liuyazhuang * */public class Objectimpl implements Object {
   public void Action () {        System.out.println ("========");        System.out.println ("========");        System.out.println ("This is the class being represented");        System.out.println ("========");        System.out.println ("========");}    }

Test
Package com.lyz.design.proxy;/** * Test * @author Liuyazhuang * */public class Test {public    static void main () {
   
    object obj = new Proxyobject ();        Obj.action ();    }}
   

Result

This is the proxy class proxy start ================ which is the proxy class ================ proxy end


A brief talk on the Java design mode--agent mode (proxy)

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.