Java Dynamic Agent

Source: Internet
Author: User

Recently, I was just looking, special to dig graves.

About the dynamic proxy design pattern it is likely that the main problem is unknowingly used, such as interceptors in spring aop,struts2.

Let's look at the static proxy mode code:

public interface Subject

{

public void dosomething ();

}

public class Realsubject implements Subject

{

public void DoSomething ()

{

System.out.println ("Call DoSomething ()");

}

}

public class Subjectproxy implements Subject

{

Subject Subimpl = new Realsubject ();

public void DoSomething ()

{

Subimpl.dosomething ();

}

}

public class Testproxy

{

public static void Main (String args[])

{

Subject sub = new Subjectproxy ();

Sub.dosomething ();

}

}

At first, I would think that the definition of subjectproxy is purely superfluous, the implementation of the direct instantiation of the completion of the operation is not a knot it?

Later, as the business is large, you will know that the implementation of the proxy class on the real class packaging for the granularity of control has important significance.

But the static proxy model itself has a big problem, if the number of class methods more and more time, the proxy class code is very large.

So the dynamic agent is introduced to solve this kind of problem.

Look at the code first:

public interface Subject

{

public void dosomething ();

}

Package test;

public class Realsubject implements Subject

{

public void DoSomething ()

{

System.out.println ("Call DoSomething ()");

}

}

Import Java.lang.reflect.InvocationHandler;

Import Java.lang.reflect.Method;

Import Java.lang.reflect.Proxy;

public class Proxyhandler implements Invocationhandler

{

Private Object tar;

Binds the delegate object and returns the proxy class

public object bind (object tar)

{

This.tar = tar;

Binds all interfaces implemented by this class, obtains the proxy class

Return Proxy.newproxyinstance (Tar.getclass (). getClassLoader (),

Tar.getclass (). Getinterfaces (),

this);

}

public object invoke (object proxy, Method method, object[] args) throws Throwable

{

Object result = null;

So we can do the so-called AOP programming here.

Perform function processing before invoking a specific function method

result = Method.invoke (Tar,args);

After invoking the specific function method, perform the function processing

return result;

}

}

public class Testproxy

{

public static void Main (String args[])

{

Proxyhandler proxy = new Proxyhandler ();

Bind all interfaces implemented by this class

Subject sub = (Subject) proxy.bind (New Realsubject ());

Sub.dosomething ();

}

}

After reading the code, let me now answer, what is the role of dynamic agent:

The code amount of the proxy class is fixed, not because the business is gradually large and large;

AOP programming can be implemented, in fact static proxies can also be implemented, in general,

AOP can be counted as a typical application of proxy mode;

Decoupling, through the parameters can be judged real class, no need to instantiate in advance, more flexible and changeable.

Welcome to Add.

The man who stole the cows on rainy night

Links: http://www.zhihu.com/question/20794107/answer/23330381

Source: Know

Java Dynamic Agent

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.