Proxy mode
I. Overview
A proxy is a pattern that provides an indirect way to access a target object, that is, through a proxy. So it is easy to add additional functions on the basis of target implementation, pre-intercept, post-intercept and so on to meet their business needs, while the proxy mode facilitates the expansion of target object function features are also used by many people.
Second, graphic description
Third, static proxy
The implementation of static proxies is relatively simple, the proxy class implements the same interface as the target object and maintains a proxy object in the class. Through the constructor to plug into the target object, assign to the proxy object, and then execute the proxy object implementation of the interface method, and realize the pre-interception, after interception and other required business functions.
/** * Interface implemented by target object * @author Jiyukai */public interface Bussinessinterface {void execute ();} /** * Target object implementation class * @author Jiyukai */public class bussiness implements bussinessinterface{@Overridepublic void execute () {Sys TEM.OUT.PRINTLN ("Execute business logic ...");} /** * proxy class, through the implementation of the same interface as the target object * and maintain a proxy object, passing through the constructor to the actual target object and assigning a value * Implement the proxy object implementation of the interface method to achieve the target object implementation of the intervention * @author Jiyukai */public class Bussin Essproxy implements Bussinessinterface{private Bussinessinterface Bussinessimpl; public Bussinessproxy ( Bussinessinterface bussinessimpl) {This.bussinessimpl = Bussinessimpl;} @Overridepublic void Execute () {System.out.println ("pre-intercept ..."); Bussinessimpl.execute (); System.out.println ("post-intercept ...");}
Summary of static agents
Advantages: Can not modify the target object under the premise of the target object to expand and intercept the function.
Disadvantage: Because the proxy object, need to implement the same interface as the target object, will lead to a very large number of proxy classes, difficult to maintain, and once the interface increases the method, the target object and the proxy class need to maintain.
Four, dynamic agent
Dynamic proxy refers to the dynamic construction of proxy objects in memory (which requires us to define the type of interface implemented by the target object to be proxied), that is, the use of the JDK API to generate the object of the specified interface, also known as the JDK proxy or interface agent.
/** * Dynamic proxy object, no need to implement any interface * by passing in any type of target object and specifying interface * Call JDK interface dynamically create proxy object * @author Jiyukai */public class Proxyfactory {Private Object Targetobject;public proxyfactory (Object targetObject) {this.targetobject = TargetObject;} Public Object getproxyinstance () {return proxy.newproxyinstance (Targetobject.getclass (). getClassLoader (),// The class loader of the target object remains consistent with the Targetobject.getclass (). Getinterfaces (),//interface implemented by the target object, because the object needs to be dynamically generated based on the interface New Invocationhandler () {// Invocationhandler: Event handler, which is the execution of the target object method @overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {System.out.println ("pre-intercept ..."), Object result = Method.invoke (proxy, args); System.out.println ("post-intercept ..."); return result;});}}
Summary of dynamic Agents
Advantages: Proxy objects do not need to implement interfaces, eliminating the hassle of writing a lot of proxy classes, while the interface addition method does not need to maintain the target object and proxy objects, simply add the method in the event handler judgment.
Disadvantage: The proxy object does not need to implement the interface, but the target object must implement the interface, otherwise the JDK dynamic agent cannot be used.
Java static agents and dynamic proxies