JAVA design mode: Proxy)

Source: Internet
Author: User

Proxy mode provides a proxy for other objects to control access to this object.
In some cases, an object does not want to or cannot directly reference another object, and the proxy object can play a mediation role between the client and the target object.

The idea of proxy mode is to insert a proxy object between the actual object and the caller to provide additional processing or different operations. These additional operations usually need to communicate with the actual object.

The author summarizes the two core contents of the proxy mode: one is to isolate the direct interaction between the visitor and the accessed object and execute all operations on the accessed object through the proxy object, this is similar to the decoration mode and appearance mode. On the other hand, the proxy object modifies the business logic of the proxy object, which can add and shield some business logic, this is not allowed by the decoration and appearance modes.

The most widely used scenario in proxy mode is to pre-and post-process business access. For example, there was a singer singing target. Now the singer is going to perform on stage and report the scenes and curtain calls before and after singing. Not every time a singer performs on stage, it is obviously inappropriate to directly modify the singing class and report the curtain and the curtain call. Here we use the proxy mode to solve this problem:

[Java]
Interface Sing {
Void sing ();
}
 
Class Fancy implements Sing {
 
@ Override
Void sing (){
System. out. println ("singing! ");
}
 
}
 
Public class FancyProxy implements Sing {
Sing singger;
 
Public FancyProxy (Sing singger ){
This. singger = singger;
}
 
@ Override
Public void sing (){
// Report
BaoMu ();
// Singing
Singger. sing ();
// Curtain call
XieMu ();
}
}
The above method solves the issue of performance on stage, but this solution must be completed in the design stage and has great limitations. JDK provides a tool for dynamically creating proxies so that we can generate an object proxy at runtime. Unlike the code above, this is what we often call dynamic Proxy: www.2cto.com.

[Java]
Class DynamicProxy implements InvocationHandler {
Private Object target;
 
Public DynamicProxy (Object target ){
This.tar get = target;
}
 
Public Object invoke (Object proxy, Method method, Object [] args)
Throws Throwable {
System. out. println ("before calling" + method );
Method. invoke (target, args );
System. out. println ("after calling" + method );
Return null;
}
 
Static void main (String [] args ){
Fancy fancy = new Fancy (); // The proxy class specified here
InvocationHandler handler = new DynamicProxy (fancy); // initialize the proxy class
Class cls = fancy. getClass ();
Sing sing = (Sing) Proxy. newProxyInstance (cls. getClassLoader (),
Cls. getInterfaces (), handler );
Sing. sing ();
}
}
In this way, the proxy object (Fancy) can be dynamically changed at runtime, and the interface (Sing Interface) to be controlled can be changed at runtime. The control method (DynamicProxy class) it can also be dynamically changed to achieve a Flexible Dynamic proxy relationship.

 

By fancy888

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.