java--proxy mode [forwarding]

Source: Internet
Author: User

1. Introduction

Proxy pattern is one of the most common design patterns of Gof 23 java. Proxy mode definition: Provide a surrogate or placeholder for another object to controlaccess to it ( provides a proxy for other objects to control access to this object ). Use proxy mode to create a proxy object that controls access to the target object (the target object can be a remote object, create an expensive object, or an object that requires security control), and can add some additional functionality without changing the target object.

2. UML Class Diagram

3. Roles included in the pattern and their responsibilities

Subject: Abstract topic role, abstract topic class can be abstract class, or interface, is a most common type of business definition, no special requirements.

Realsubject: The specific subject role, also called the delegated role, the agent role. Are the specific performers of the business logic.

Proxy: Agent topic role, also known as Delegate class, proxy class. It brings all the methods defined by the abstract topic class to the specific theme role, and does preprocessing and aftercare before and after the specific topic roles are processed. (simplest, such as print logs)

4. Code implementation

(1) Subject

Package com.jackie.designpatterns.proxy;

/**

* Abstract themes, defining key Features

*/

Publicinterface Subject {

publicvoid operate ();

}

(2) Realsubject

Package com.jackie.designpatterns.proxy;

/**

* Specific Topics

*/

Publicclass Realsubject implements subject{

@Override

publicvoid operate () {

System. out. println ("Realsubject operatestarted ...");

}

}

(3) Proxy

Package com.jackie.designpatterns.proxy;

/**

* Proxy class

*/

Publicclass Proxy implements subject{

Private Subject Subject;

Public Proxy (Subject Subject) {

this. Subject = subject;

}

@Override

publicvoid operate () {

System. out. println ("before operate ...");

Subject.operate ();

System. out. println ("after operate ...");

}

}

(4) Client

Package com.jackie.designpatterns.proxy;

/**

* Customer

*/

Publicclass Client {

/**

* @param args

*/

publicstaticvoid Main (string[] args) {

Subject Subject = new realsubject ();

Proxy proxy = new proxy (subject);

Proxy.operate ();

}

}

Running Result: Beforeoperate ...

Realsubject operate started ...

Afteroperate ...

5. Application Scenarios

In the real world, the Secretary is the equivalent of an agent, the boss meeting, then inform the staff meeting time, decorate the venue, after the meeting and other meetings related work can be handed to the secretary to do, the boss just need to have a meeting on the line, do not need to do those things personally. Similarly, in our programming we can also use the proxy pattern to decouple the code that is composed of a series of unrelated logic, such as the log code in the business code can be done in the agent. Spring's AOP is a typical dynamic proxy application.

6. Application form of proxy mode

(1) Remote proxy-You can hide the fact that an object exists in a different address space. Also allows the client to access objects on the remote machine, the remote machine may have better computational performance and processing speed, and can quickly respond to and process client requests.

(2) Virtual proxy – allows objects with large memory overhead to be created when needed. It is only created when we really need this object.

(3) Copy-on-write agent (Copy-on-write proxy) – Used to control the replication of objects by delaying the copying of objects until the customer really needs them. is a variant of the virtual agent.

(4) Protection agent (Protection (access) proxy) – provides different levels of access to target objects for different customers

(5) Cache proxy – provides temporary storage for the result of a costly operation that allows multiple customers to share results to reduce compute or network latency.

(6) Firewall proxy (Firewall proxy) – Control access to network resources and protect topics from malicious customers.

(7) Synchronous agent (Synchronizationproxy) – Provides secure access to topics in multi-threaded situations.

(8) Smart Reference Agent (Smart Referenceproxy)-When an object is referenced, provides some extra action, such as recording the number of times this object is called.

(9) Complex hidden proxy (complexity Hidingproxy) – The complexity of hiding a complex set of classes and access control. Sometimes called the appearance agent (Façade proxy), it is not difficult to understand. Complex hidden proxies and appearance patterns are not the same because the proxy controls access, and the appearance pattern is not the same because the proxy controls access, while the façade mode only provides another set of interfaces.

Source: http://blog.csdn.net/jackiehff/article/details/8621517

java--proxy mode [forwarding]

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.