Java Static agent and dynamic agent in layman's

Source: Internet
Author: User

First of all, what is a proxy:

proxy mode, is a common design pattern. The characteristic is that the proxy class and the delegate class have the same interface, and the proxy class is mainly responsible for preprocessing the message, filtering the message, and forwarding the message to the delegate class. and post-processing messages.  

There is an association between the proxy class and the delegate class. The object of the proxy class itself does not really implement the service, knowledge by invoking the object's related methods of the delegate class.

proxy classes can be divided into two types: static agents and dynamic proxies.

Static Proxy:

The proxy class is compiled by the programmer, or by the code generated by the tool. The *.class file for the proxy class already exists before the program runs. You can run it directly.

Dynamic Agent:

The proxy class for the dynamic agent. Not directly generated by source code. The object of the dynamic proxy class is generated dynamically by the Java reflection mechanism when the program is run, without the need to write the source code manually. This improves the scalability of the software. The Java reflection mechanism can generate any type of dynamic proxy class.

Implementation of static proxies:

Interface:

Package Test.static.pattern;public interface Usermanager {public void AddUser (String userid,string userName);p ublic void Deluser (string userid);p ublic string Finduser (string userid);p ublic void ModifyUser (String userid,string usernamestring);}


Implementation class:

Package Test.static.pattern;public class Usermanagerimpl implements Usermanager {@Overridepublic void AddUser (String UserID, String userName) {System.out.println ("Usermanagerimpl.adduser ()  userid-->>" + userId);} @Overridepublic void Deluser (String userId) {System.out.println ("Usermanagerimpl.deluser () userid-->>" + userId );} @Overridepublic string Finduser (String userId) {System.out.println ("Usermanagerimpl.finduser () userid-->>" + USERID); return null;} @Overridepublic void ModifyUser (String userId, String usernamestring) {System.out.println (" Usermanagerimpl.modifyuser () userid-->> "+ UserId";}}

Static proxy class: (only holds references to objects)

Package Test.static.pattern;public class Usermanagerimplproxy implements Usermanager {private Usermanager usermanager; Public Usermanagerimplproxy (Usermanager usermanager) {This.usermanager=usermanager;} @Overridepublic void AddUser (string userid, String userName) {usermanager.adduser (userid, userName);} @Overridepublic void Deluser (String userid) {usermanager.deluser (userid);} @Overridepublic string Finduser (string userid) {return usermanager.finduser (userId); @Overridepublic void ModifyUser (string userid, String usernamestring) {usermanager.modifyuser (userid, usernamestring) ;}}

Client:

Package Test.static.pattern;public class Client {/** * @param args */public static void main (string[] args) {//Normal thinking: Client direct real Instantiate the object of the subclass Usermanager Usermanager=new Usermanagerimpl ();//static Proxy: Client instantiation proxy, referral to subclass by proxy Usermanager Usermanager=new Usermanagerimplproxy (New Usermanagerimpl ()); Usermanager.adduser ("0001", "Zhang San");}}

Execution Result:

A static proxy, because the proxy class holds a reference to the object, so it can be controlled.

Symptom 1: For each function module, the corresponding proxy class should be established. resulting in a large number of proxy classes

Symptom 2: But if you have the same control over each implementation, you need to write a large amount of code repeatedly.

To avoid repeating the code multiple times, let's look at what a dynamic proxy is.


The implementation of the dynamic agent:

Interface:

Package Test.dynamic.pattern;public interface Usermanager {public void AddUser (String userid,string userName);p ublic void Deluser (string userid);p ublic string Finduser (string userid);p ublic void ModifyUser (String userid,string usernamestring);}


Implementation class:

Package Test.dynamic.pattern;public class Usermanagerimpl implements Usermanager {@Overridepublic void AddUser (String UserID, String userName) {System.out.println ("Usermanagerimpl.adduser ()  userid-->>" + userId);} @Overridepublic void Deluser (String userId) {System.out.println ("Usermanagerimpl.deluser () userid-->>" + userId );} @Overridepublic string Finduser (String userId) {System.out.println ("Usermanagerimpl.finduser () userid-->>" + USERID); return null;} @Overridepublic void ModifyUser (String userId, String usernamestring) {System.out.println (" Usermanagerimpl.modifyuser () userid-->> "+ UserId";}}


Dynamic Proxy Generation classes:

Package Test.dynamic.pattern;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.proxy;import Org.omg.corba.systemexception;import Org.omg.corba.portable.inputstream;import Org.omg.corba.portable.invokehandler;import Org.omg.corba.portable.outputstream;import Org.omg.corba.portable.responsehandler;public class Loghandler implements Invocationhandler {/** * Invoke method abstraction for referencing object classes */@ Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {object Ret=null;try {//Call target method re T=method.invoke (TargetObject, args);} catch (Exception e) {throw e;} return null;}  A reference to the target implementation private Object targetobject;/** * Proxy class Generation method * @param targetObject * @return */public object Newproxyinstance (object TargetObject) {This.targetobject=targetobject;return proxy.newproxyinstance (Targetobject.getclass (). getClassLoader (), Targetobject.getclass (). Getinterfaces (), this);}}


Client:

Package Test.dynamic.pattern;public class Client {/** * @param args */public static void main (string[] args) {//dynamic proxy Loghan Dler loghandler=new Loghandler (); Usermanager usermanager= (Usermanager) loghandler.newproxyinstance (New Usermanagerimpl ());//userManager.addUser (" 0001 "," Zhang San "); Usermanager.deluser (" 111 ");}}

Execution Result:

dynamic proxy class, not in the program embodied. Create the appropriate proxy class only when the program is running. This makes it possible to reduce the number of proxy classes. for Phenomena 2 , how the same control code is reduced. Take a look at the next blog post.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Static agent and dynamic agent in layman's

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.