Static proxy of Java proxy mode

Source: Internet
Author: User
Tags add time

As a novice developer , you may not be exposed to proxy mode, but in many frameworks the use of proxy mode is unknowingly used, such as the servlet filter chain, Spring AOP, and Spring MVC Interceptor. Therefore, understanding the proxy model is unavoidable for personal growth.

In some cases, a customer does not want or cannot refer directly to an object, and an indirect reference can be achieved by a third party called a "proxy". A proxy object can act as a mediator between the client and the target object, and can remove content and services that the customer cannot see or add additional services that the customer needs through the proxy object.

The original text is discussed with the author: http://www.cnblogs.com/intsmaze/p/6013461.html

Sina Weibo: Intsmaze Liu Yangyang

by introducing a new object to implement the operation of the real object or using the new object as a surrogate for the real object, this implementation mechanism is the proxy mode, which indirectly accesses an object by introducing the proxy object, which is the mode motive of the proxy mode.   Proxy Pattern: Provides a proxy for an object and controls a reference to the original object by the proxy object. Proxy mode in English is called proxy or surrogate, it is an object-structured pattern.

proxy mode schematic diagram is relatively simple, generally can be simplified as shown, but in reality is much more complex.
 The following scenario is used to understand why an aggregation is taken instead of an inherited invocation:
 Public InterfaceMoveable {voidmove (); voidstop ();} Public classTankImplementsMoveable { Public voidMove () {System.out.println ("Tank Move"); Try{Thread.Sleep (NewRandom (). Nextint (10000)); } Catch(interruptedexception e) {e.printstacktrace (); }            }         Public voidStop () {System.out.println ("Tank Stop"); }    }  
now we want to add a function, record the execution time of the Move method, cannot modify the source code.
Mode 1: Adopt Inheritance
 Public class extends Tank {    publicvoid  Move () {                System.out.println ("time Start");         Super . Move ();        System.out.println ("Time End");    }    }  

Add a function, record the move's execution before and after the log information, can not modify the source code.

 Public class extends Tank {    publicvoid  Move () {                System.out.println ("log Start");         Super . Move ();        SYSTEM.OUT.PRINTLN ("Log End");    }    }  
But if you want to record the log at the time of recording, then you need to create a class to inherit the Tank1 class. to record time before logging, create a class that inherits the Tank2 class. The proxy mode does not take inheritance mode because of poor extensibility.
method Two: Recommend the use of aggregation mode
 Public classTimeproxyImplementsmoveable {moveable m;  PublicTimeproxy (moveable m) {Super();  This. m =m; }     Public voidMove () {System.out.println ("Time Start");        M.move (); System.out.println ("Time End"); }    }  Public classLogproxyImplementsmoveable {moveable m;  PublicLogproxy (moveable m) {Super();  This. m =m; }     Public voidMove () {System.out.println ("Log Start");        M.move (); System.out.println ("Log End"); }    }

Want to add log function

   Public Static void throws Exception {        new  Tank ();        Moveable m=new  logproxy (t);        M.move ();    }

Want to log again time, directly at the call to combine , do not need to create a new class

 Public Static void throws Exception {        new  Tank ();        Moveable m=new  logproxy (t);        Moveable s=new  timeproxy (m);        S.move ();    }

If you want to add time calculations to all the methods in the moveable interface

 Public classTimeproxyImplementsmoveable {moveable m;  PublicTimeproxy (moveable m) {Super();  This, mb=m; }     Public voidMove () { This. before ();        M.move ();  This. After (); } @Override Public voidStop () { This. before ();        M.stop ();  This. After (); }         Public voidbefore () {System.out.println ("Time Start"); }     Public voidAfter () {System.out.println ("Time End"); }}
Advantages of the proxy modeThe proxy mode can coordinate the caller and the callee, and reduce the coupling degree of the system to some extent. remote agents enable clients to access objects on remote machines, and remote machines may have better computational performance and processing speed, and can quickly respond to and process client requests. virtual agents represent a large object by using a small object, which can reduce the consumption of system resources, optimize the system and improve the speed of operation. The protection agent can control access to real objects. Disadvantages of the proxy modebecause proxy objects are added between the client and the real topic, some types of proxy patterns can cause requests to be processed more slowly. implementing the proxy mode requires additional work, and some proxy patterns are very complex to implement.

When I was 8, I was going to explain the implementation of proxy mode's high-order application dynamic agent.

Static proxy of Java proxy mode

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.