Java Reflection Learning Summary three (static proxy)

Source: Internet
Author: User

The most common application of reflection is the proxy mode.

This article introduces the proxy mode briefly, and writes a static proxy example. For the next important dynamic agent to do some groundwork


the role of the proxy mode is :

Provides a proxy for other objects to control access to this object.
Also, in some cases, a client does not want or cannot directly reference another object, and the proxy object can act as an intermediary between the client and the target object.
Frankly speaking, the proxy mode is an "intermediary", he has this customer references, so you can use this "intermediary" to replace the customer to do the operation.


Role of proxy mode design :
To use proxy mode, you need to define the following 3 roles:

Abstract Roles :

Common interface for declaring real objects and proxy objects

Real Objects :

The real object represented by the proxy role is the object we end up referencing

Agent Role :

1. The proxy object contains a reference to the real object, allowing you to manipulate the real object.
2. The proxy object provides the same interface as the real object in order to replace the real object at any moment.
3. Proxy objects can attach other actions when performing real object operations, which is equivalent to encapsulating real objects


May be a little confused after reading the concept, the next direct code to see the example should be relatively easy to understand:

1. first define an abstract role object, using an abstract class or interface to define a method common to both a proxy role and a real object. This method is a method that is delegated to the "mediation" by the agent role

[Java]View Plaincopy print?
    1. This defines an abstract class
    2. Public Abstract class Subject {
    3. //Null abstract method, this method requires both the proxy role and the real role to implement it
    4. public abstract void request ();
    5. }


2. defining real-world role objects

[Java]View Plaincopy print?
    1. The real role object, inherited from the abstract role, overrides the defined method.
    2. Public class Realsubject extends subject{
    3. //Implement the required action in the overridden method, here is a simple print
    4. @Override
    5. public void request () {
    6. System.out.println ("This is real subject");
    7. }
    8. }

3. Defining a Proxy role object

[Java]View Plaincopy print?
  1. Proxy roles, which also inherit abstract roles, override methods in abstract roles
  2. Public class Proxysubject extends subject{
  3. //A reference to a real role within a proxy role
  4. private Realsubject Realsubject;
  5. //Override methods in the implementation abstract role
  6. @Override
  7. public void request () {
  8. this.prerequest (); //operations that are attached before invoking a real role operation
  9. if (realsubject = = null) {
  10. Realsubject = new Realsubject ();
  11. }
  12. Realsubject.request (); //What the real character has done
  13. this.postrequest (); //actions attached after a real role operation
  14. }
  15. //Custom method that is called before the real method executes
  16. private void Prerequest () {
  17. System.out.println ("pre Request");
  18. }
  19. //Custom method that is called after the real method executes
  20. private void Postrequest () {
  21. System.out.println ("POST request");
  22. }
  23. }

The most important thing to do when implementing a proxy role object is to have a reference to the real object.

Using this reference to invoke the real object in the proxy object, and you can customize some other operations, such as Prerequest () and Postrequest () in this example, they are called before and after each.

This can be used to record the run time or log operations, but this is just a word printed.


4. finally see how the Main method calls:

[Java]View Plaincopy print?
    1. public class  testproxy {  
    2.   
    3.     < span class= "keyword" >public static void main (string[ ] args)  {  
    4.         < span class= "comment" >//instantiate a proxy role object   
    5.          subject subject = new proxysubject ();   
    6.         //use proxy object to invoke a generic method of definition   
    7.         subject.request ();  
    8.     }  
    9. }&NBSP;&NBSP;

The output is:

Pre Request

This is real subject

POST request

When you look at the code, you can see that there is no knowledge of reflection. And for the example above, the real role object must already exist beforehand and must be an intrinsic property of the proxy object.

If so, there is a real role that must have a reference in the agent role, if you do not know the real role of the situation and what to do? This requires a "dynamic agent" to solve.

The next article is about " dynamic Agents ".


Java Reflection Learning Summary three (static 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.