Java static proxy

Source: Internet
Author: User

1. Definition

Provides a proxy for other objects to control access to this object. In some cases, an object does not fit or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object. For example, when renting, the intermediary acts as an agent.

2. Static proxy role

Abstract role: A business method that declares the real role implementation through an interface or abstract class.

Real role: The role of the agent, the implementation of abstract roles, define the real role of the business logic to be implemented for the role of the proxy call.

Proxy role: Proxy real role, is the real role of the agent, through the real role of the business logic method to implement the abstract method, and can attach their own operations.

3. Example Analysis

We use the rental house as an example to explain what is called static proxy.

Hostrent.java abstract Role class.

1  Public Interface hostrent {2     // How to rent 3      Public void rent (); 4 }
View Code

Host.java real Role (landlord)

1  Public classHostImplementshostrent {2 3 @Override4      Public voidrent () {5         //TODO auto-generated Method Stub6System.out.println ("House for Rent!"));7         8     }9     Ten}
View Code

Proxy.java Agent Role (mediation)

1  Public classProxyImplementshostrent{2     Privatehost host;3 @Override4      Public voidrent () {5         //TODO auto-generated Method Stub6 host.rent ();7     }8     9      Public voidSethost (host host) {Ten          This. Host =host; One     } A      -      PublicProxy (host host) -     { the          This. Host =host; -     } -      -}
View Code

Client.java Customer

 1  public  class       Client { 2  public  static  void   Main (string[] args) { 3  host host = 
   
    new  
     Host ();  
    4  proxy proxy = 
    new  
     Proxy (host);  
    5  
     Proxy.rent ();  
    6  
    }< /span>8 } 
   
View Code

In the example here, the intermediary represents a proxy role. The advantage is that the real character handles the business more purely, and no longer focuses on public things. From the above example, we may not see any advantage, let us cite an example.

User.java Abstract Role Classes

1  Public InterfaceUser {2     //Add Data3      Public voidAdd ();4     //Update Data5      Public voidupdate ();6     //Delete Data7      Public voidDelete ();8     //Querying Data9      Public voidSelect ();Ten}
View Code

Userimpl.java

1  PackageCom.spring.staticProxy;2 3  Public classUserimplImplementsUser {4 5 @Override6      Public voidAdd () {7SYSTEM.OUT.PRINTLN ("Add Data");8     }9 Ten @Override One      Public voidUpdate () { ASYSTEM.OUT.PRINTLN ("Update Data"); -     } -  the @Override -      Public voidDelete () { -System.out.println ("Delete data"); -          +     } -  + @Override A      Public voidSelect () { atSYSTEM.OUT.PRINTLN ("Query data"); -     } -      -}
View Code

We want to add the log function to the Userimpl class, and then we have to add the log function to each method as follows

1  Public classUserimplImplementsUser {2 3 @Override4      Public voidAdd () {5SYSTEM.OUT.PRINTLN ("Add Data");6     }7 8 @Override9      Public voidUpdate () {TenLog ("Update"); OneSYSTEM.OUT.PRINTLN ("Update Data"); A     } -  - @Override the      Public voidDelete () { -Log ("delete"); -System.out.println ("Delete data"); -          +     } -  + @Override A      Public voidSelect () { atLog ("select"); -SYSTEM.OUT.PRINTLN ("Query data"); -     } -      -     Private voidlog (String methodName) -     { inSystem.out.println ("Execute" +methodname+ "method"); -     } to      +}
View Code

At this time, the Userimpl class becomes not pure, inside the logic business, there are many public parts of the code, then need an agent, the agent to implement the log function. The entire proxy class is as follows

1  Public classProxyImplementsuser{2     3     PrivateUserimpl user;4 @Override5      Public voidAdd () {6Log ("Add");7 User.add ();8     }9 Ten @Override One      Public voidUpdate () { ALog ("Update"); - user.update (); -     } the  - @Override -      Public voidDelete () { -Log ("delete"); + User.delete (); -     } +  A @Override at      Public voidSelect () { -Log ("select"); - User.select (); -     } -      -     Private voidlog (String methodName) in     { -System.out.println ("Execute" +methodname+ "method"); to     } +}
View Code

The entire proxy class realizes the log function, avoids the processing logic business in the real class, makes the whole real class more pure, only has the operation function to the database, and extracts the public part. This is the static proxy.

in practice, a real role must correspond to a proxy role, but if used in a large amount of time, it can cause a drastic expansion of the class. For example, to proxy the host class and the Userimpl class, you need two proxy classes. The dynamic proxy class can solve this problem, he only need a proxy class to complete. The following are the advantages and disadvantages of static proxy classes:

Advantages:

1, make the real character more pure, no longer pay attention to some public business.

2, the public service is done by the agent class to achieve the division of business.

3, when the business development, only need to modify the proxy class, become more and more convenient

Disadvantages:

1, more agent class, the workload becomes larger, the development efficiency becomes lower

  

 

Java 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.