Java implementation Proxy

zhaozj2021-02-16  160

When reading Gigix's G-ROLLER, the Proxy mode is used to achieve transaction processing to the business. In fact, there are many design patterns in Java's SDK, and Observer is also one of them. But because such APIs are rare, they have no opportunity to learn. And the example is the best teacher.

Package com.bonjovi.test;

Import java.lang.reflect.invocationHandler; import java.lang.reflect.Proxy; import java.lang.reflect.method; import java.util.vector; import java.util.list;

Public Class VectorProxy Implements InvocationHandler {Private Object Proxyobj;

/ ** @Link Dependency * / / * #proxy lnkProxy; * /

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

Object o = method.invoke (proxyobj, args);

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

Return O;

Public static void main (string [] args) {list v = null;

V = (list) Factory (New Vector (10));

v.add ("new"); v.add ("york");}}

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

New Post(0)