The proxy pattern of the Java design pattern

Source: Internet
Author: User

I. Overview

Provides a proxy for an object and is accessed by the proxy object to complete the original object. proxy mode is an object-structured pattern .


Second, the application scenario

when there is no direct access to an object or access to an object is difficult to access indirectly through a proxy object, in order to ensure the transparency of client use, the delegate object and the proxy object need to implement the same interface.


Third, UML class diagram


Iv. participants

1. interface class : Subject

It declares a common interface between a real visitor and a proxy visitor, and the client typically needs to program for the interface role.

2, agent class : Proxysubject

Contains a reference to the real (delegate) object (Realsubject), which invokes the interface method execution of the referenced object in the implemented interface method, thus achieving the role of the agent. It looks like the proxy object (Proxysubject) is in action, but actually the actual operator is the delegate object (Realsubject).

3. delegate class/Real Access class : Realsubject

It defines the real object represented by the proxy role, implements real business operations in the real role, and the client can indirectly invoke the actions defined in the real role through the proxy role.


V. Use case Learning

1. Interface class: Subject.java

/** * Interface class * @author [email protected] * */public interface Subject {public void visit ();}
2, interface implementation class,Real Access Object/delegate object:Realsubject.java

/** * Interface implementation class, real Access Object/delegate Object * @author [email protected] * */public class Realsubject implements Subject {@Overridepublic void Visit () {System.out.println ("I am ' realsubject ', I am the execution method");}}
3, interface implementation class, proxy object:Proxysubject.java

/** * Interface implementation class, proxy object * @author [email protected] * */public class Proxysubject implements Subject {//Maintain a reference to a real delegate object that is the real performer Private Subject realsubject;public Proxysubject (Subject Subject) {this.realsubject = Subject;} @Overridepublic void Visit () {///Real delegate object indirectly implements access to the target object through a reference to the proxy object Realsubject.visit ();}}
4.Customer Class Client.java

/** * Customer class * @author  [email protected] * */public class Client {public static void main (string[] args) {Subject proxysu Bject = new Proxysubject (new Realsubject ());p roxysubject.visit ();}}

Vi. Others/Extensions

By the time the proxy class was created, the proxy class could be divided into two types:

1. static proxy : The source code is generated automatically by the programmer or a specific tool, and then compiled. Before the program runs, the. class file for the proxy classes already exists. (The above use case is about static proxy technology)

Analysis of the advantages and disadvantages of static agents:

Advantages: The Client interface programming, conforms to the open and close principle, makes the system has the good flexibility and the expansibility.

Disadvantage: From the above code we can find that each kind of proxy class is to implement a specific interface, and each proxy class can only be a specific interface for the implementation of the class proxy . If it is another implementation class under a different interface, you will need to redefine the proxy class under the new interface.

Is it possible to perform the proxy operation of the class under different interfaces through a proxy class? Then you must use dynamic Proxy to do this.


2, dynamic agent : When the program runs, the use of Java reflection mechanism to dynamically create proxy instances.

"Notice: About dynamic Agent technology will be detailed in the next article"



—————————————————————————————————————

if the content of the article is helpful to you, do not forget to support it by the top!  

If you have any questions about the content of the article or have a better opinion, you can contact me by commenting or sending a message:

[Email protected]/[email protected]


If you need to reprint, please indicate the source, thank you!!

—————————————————————————————————————


The proxy pattern of the Java design 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.