Design pattern --Proxy model

xiaoxiao2021-03-06  77

This is an example of a dynamic agent. It is late today. I will analyze the time.

Package javapatterns;

Import java.lang.reflect.invocationhandler; import java.lang.reflect.method; import java.lang.reflect.Proxy; import java.util.list; import java.util.vector; / ** * proxy model research *

Title: VectorProxy.java *

Description: *

Copyright: Copyright (C) 2004 DSII, Inc *

Company: DSII, INC < / p> * @Author Morgan 2004-11-8 * @version 1.0 * / public class vectorproxy imports infocationhandler {private object proxy {private object proxy;

Public VectorProxy (Object Obj) {proxyobj = Obj;}

Public Static Object Factory (Object Obj) {Class CLS = Obj.getClass ();

Return Proxy.newProxyInstance (CLS.GetClassLoader (), Cls.GetInterface (), New VectorProxy (Obj));}

Public Object Invoke (Object Proxy, Method Method, Object [] args "throws throwable {system.out.println (" before calling " method);

IF (args! = null) {for (int i = 0; i

System.out.println ("After Calling" Method; Return O;

Public static void main (string [] args) {list v = null; v = (list) Factory; v.add ("new"); v.Add ("york");}} operation result:------------------------------------------

before calling public abstract boolean java.util.List.add (java.lang.Object) Newafter calling public abstract boolean java.util.List.add (java.lang.Object) before calling public abstract boolean java.util.List.add (java.lang.object) YORKAFTER CALLING PUBLIC Abstract Boolean Java.util.List.Add (java.lang.Object)

----------------------------------------- The above example, the core implementation is actually JDK The dynamic agent. This is also the core principle of Spring's AOP (of course, there is also cglib). I understand that you can quickly understand the principles of Spring's AOP. From above, you can implement dynamic agents, The java.lang.reflect.invocationhandler interface must be implemented, and the publiC Object Invoke (Object Proxy, Method Method, Object [] args) method in the InvocationHandler interface is overridden. So as long as the method of the proxy class is called, INVOKE is automatically performed. () Method. What is the use of this? --------------------------------------- - System.out.println ("Before Calling" Method; Object O = Method.Invoke (Proxyobj, Args); System.out.Println ("After Calling Method); -------- ----------------------------------- You should have seen it, we can call it by the proxy method Before and after, we can do what you want to do, such as the incoming parameter control, log information, etc. (Spring management is also like this) ........ Here, I have to remind Everyone is, if the code above is changed to List V = (Vector) Factory (New Vector (10));

The ClassCastException will be thrown downwards! Proxy.newProxyInstance (CLS.GetClassLoader (), cls.getinterface (), new vectorproxy (obj)); see CLS.GetInterfaces () not, it returned to CLS Interface instance, and in the example, we pass the proxyobj is a vector, then one of its interface is List. So it is possible to turn to List, and the vector will have an error .........

转载请注明原文地址:https://www.9cbs.com/read-94417.html

New Post(0)