Comparison between static agent and dynamic agent in Java — reprint

< pre class = "recommend text mb-10" > extension: < a href=“ http://www.ibm.com/developerworks/cn/java/j-lo-proxy1/ "> http://www.ibm.com/developerworks/cn/java/j-lo-proxy1/JAVA Comparison between static agent and dynamic agent 1. Conceptual agent mode is a commonly used java design mode. Its feature is that the agent class has the same interface with the delegate class. The agent class is mainly responsible for preprocessing messages for the delegate class, filtering messages, forwarding messages to the delegate class, and post-processing messages. There is usually an association relationship between proxy class and delegate class. The object of a proxy class is associated with the object of a delegate class. The object of the proxy class itself does not really implement the service, but provides a specific service by calling the relevant methods of the object of the delegate class. According to the creation period of the proxy class, the proxy class can be divided into two types. Static proxy class: it is created by programmers or automatically generated by specific tools, and then compiled. The of the proxy class before the program runs< a class="inner-link decor-none" href="%20http://zhidao.baidu.com/search?word=class%E6%96%87%E4%BB%B6&%20;%20fr=qb_%20search_%20exp&%20ie=utf8" rel="nofollow" target="_ blank" data-word="0 "> class file already exists. Dynamic proxy class: it is created dynamically by using reflection mechanism when the program is running. Second, static proxy classes are as follows. Helloserviceproxy class is a proxy class and helloserviceimpl class is a delegate class. Both classes implement the helloservice interface. Helloserviceproxy class is the real implementer of the helloservice interface, and helloserviceproxy class It provides a specific service by calling the relevant methods of the helloserviceimpl class. The echo () method and gettime () method of the helloserviceproxy class will call the echo () method and gettime () method of the represented helloserviceimpl object respectively, and perform some simple printing operations before and after the method call. It can be seen that the agent class can preprocess messages for the delegate class, forward messages to the delegate class, and post process messages. Routine 1 helloservice javapackage proxy; import java.util.Date;public interface HelloService{ public String echo(String msg); public Date getTime();} Routine 2 helloserviceimpl javapackage proxy; import java.util.Date;public class HelloServiceImpl implements HelloService{ public String echo(String msg){ return "echo:"+msg; } public Date getTime(){ return new Date(); }} Routine 3 helloserviceproxy javapackage proxy; import java.util.date; public class helloserviceproxy implements helloservice {private helloservice helloservice; / / indicates the delegated helloservice instance public helloserviceproxy (helloservice helloservice) {this. Helloservice = helloservice;} public void setHelloServiceProxy(HelloService helloService){ this.helloService=helloService; } Public string echo (string MSG) {system.out.println ("before calling echo()"); / / preprocess string result = helloservice.echo (MSG); / / call the echo() method system.out.println ("after calling echo()") of the delegated helloservice instance; / / post process return result;} Public date gettime() {system.out.println ("before calling gettime()"); / / preprocess date date = helloservice. Gettime(); / / call the gettime() method of the represented helloservice instance system.out.println ("after calling gettime()"); / / post process return date;}} In the main () method of the Client1 class, we first create a HelloServiceImpl object and create a HelloServiceProxy object. Finally, we call the echo () method of the HelloServiceProxy object. Routine 4 client1 javapackage proxy; public class Client1{ public static void main(String args[]){ HelloService helloService=new HelloServiceImpl(); HelloService helloServiceProxy=new HelloServiceProxy(helloService); System.out.println(helloServiceProxy.echo("hello")); }} Run the client1 class, and the print results are as follows: before calling echo() after calling echo() echo: the source code of helloserviceproxy class of Hello routine 3 is written by the programmer. Before the program runs, it is< a class="inner-link decor-none" href="%20http://zhidao.baidu.com/search?word=class%E6%96%87%E4%BB%B6&%20;%20fr=qb_%20search_%20exp&%20ie=utf8" rel="nofollow" target="_ Blank "data word =" 0 "> the class file already exists. This kind of proxy class is called static proxy class. Third, the dynamic proxy class is compared with the static proxy class. The < a class =" inner link Decor none "href =" http://zhidao.baidu.com/search?word=%E5%AD%97%E8%8A%82%E7%A0%81& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" target="_ Blank "data word =" 6 "> bytecode is dynamically generated by java reflection mechanism when the program is running, without requiring programmers to write its source code manually. Dynamic proxy class not only simplifies programming work, but also improves < a class =" inner link Decor none "href =" http://zhidao.baidu.com/search?word=%E8%BD%AF%E4%BB%B6%E7%B3%BB%E7%BB%9F& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" target="_ Blank "data word =" 3 "> the extensibility of software system, because java reflection mechanism can generate any type of dynamic proxy class. < a class =" inner link Decor none "href =" http://zhidao.baidu.com/search?word=java.lang& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" target="_ Blank "data word =" 1 "> the proxy class and invocationhandler interface in the java.lang.reflect package provide the ability to generate dynamic proxy classes. The proxy class provides < a class =" inner link Decor none "href =" http://zhidao.baidu.com/search?word=%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95&%20;%20fr=qb_%20search_%20exp&%20ie=utf8" rel="nofollow" target="_ Blank "data word =" 4 "> static method. (1) getproxyclass() < a class =" inner link Decor none "href =" http://zhidao.baidu.com/search?word=%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95&%20;%20fr=qb_%20search_%20exp&%20ie=utf8" rel="nofollow" target="_ Blank "data word =" 4 "> the static method is responsible for creating a dynamic proxy class. Its complete definition is as follows: public static class getproxyclass (classloader loader, class [] interfaces) throws illegalargumentexception parameter loader specifies the class loader of the dynamic proxy class, and parameter interfaces specifies all the interfaces that the dynamic proxy class needs to implement. (2) newproxyinstance() the static method is responsible for creating an instance of the dynamic proxy class. Its complete definition is as follows: public static object newproxyinstance (classloader, loader, class [] interfaces, invocationhandler) Throws illegalargumentexception parameter loader specifies the class loader of the dynamic proxy class, parameter interfaces specifies all interfaces to be implemented by the dynamic proxy class, and parameter handler specifies the invocationhandler object associated with the dynamic proxy class. The following two methods create instances of the dynamic proxy class that implements the foo interface: / method 1 / / create the invocationhandler object invocationhandler handler = new myinvocationhandler (...)// Create dynamic proxy class: proxyclass = proxy getProxyClass(Foo.class.getClassLoader(),new Class[] { Foo.class });// Create an instance of the dynamic proxy class foo foo = (foo) proxyclass getConstructor(new Class[] { InvocationHandler.class }). newInstance(new Object[] { handler });/ Method 2 / / create invocationhandler object invocationhandler = new myinvocationhandler (...)// Directly create an instance of the dynamic proxy class foo foo = (foo) proxy newProxyInstance(Foo.class.getClassLoader(),new Class[] { Foo.class },handler); The dynamic proxy class created by the static method of the proxy class has the following characteristics: the dynamic proxy classes are public, final, and non - < a class = "inner link Decor none" href=“ http://zhidao.baidu.com/search?word=%E6%8A%BD%E8%B1%A1%E7%B1%BB& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" target="_ Blank "data word =" 7 "> abstract type; dynamic proxy class inherits < a class =" inner link Decor none "href =" http://zhidao.baidu.com/search?word=java.lang& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" target="_ Blank "data word =" 1 "> java.lang.reflect.proxy class; the name of the dynamic proxy class starts with" $proxy "; the dynamic proxy class implements all interfaces specified by the parameter interfaces in the getproxyclass() and newproxyinstance() methods; isproxyclass of the proxy class (class CL) Static methods can be used to determine whether the class specified by the parameter is a dynamic proxy class. Only classes created through proxy classes are dynamic proxy classes; Dynamic proxy classes all have a < a class = "inner link Decor none" href of public type=“ http://zhidao.baidu.com/search?word=%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" target="_ Blank "data word =" 5 "> construction method, which is < a class =" inner link Decor none "href =" http://zhidao.baidu.com/search?word=%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" target="_ Blank "data word =" 5 "> the constructor has a parameter of invocationhandler type. The instance of the dynamic proxy class created by the static method of the proxy class has the following characteristics: 1. Assuming that the variable foo is an instance of a dynamic proxy class and the dynamic proxy class implements the foo interface, then" foo instanceof foo " The value of is true. It is legal to cast the variable foo to foo type: (foo) foo / / 2 Each dynamic proxy class instance is associated with an invocationhandler instance. The getinvocationhandler (object proxy) static method of the proxy class returns the invocationhandler object associated with the proxy class instance specified by the proxy parameter. 3. Assuming that the foo interface has an amethod () method, when the program calls the amethod () method of the dynamic proxy class instance foo, the method will call the invoke () method of the invocationhandler object associated with it. Invocationhandler interface is a method calling interface, which declares the invoke () method responsible for calling any method: object invoke (object proxy, method method, object [] args) throws throwable parameter proxy specifies the dynamic proxy class instance, parameter method specifies the called method, and parameter args specifies the parameters passed to the called method, The return value of the invoke () method represents the return value of the called method. 4、 Finally, take a look at an example: the gethelloserviceproxy () static method of the helloserviceproxyfactory class is responsible for creating an instance of the dynamic proxy class that implements the helloservice interface. Routine 5 helloserviceproxyfactory javapackage proxy; import

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>