Java Dynamic Proxy detailed _java

Source: Internet
Author: User
Tags throwable

Agent know it, you go to buy things there are many agents, they are selling the original things. For example, you buy meat every day, pigs are farmers Uncle raised, but you buy meat from the butcher's hand, this butcher can be regarded as an agent. Then why to agent, what is the use of agents, of course, something for him to do, the agent for the butcher to understand, because you can not go to slaughter pigs, so the agent is to buy a pig, and then kill to sell you, of course, the butcher may give the meat water, the key to see his bad, so the whole process of the butcher is:

This process with the code how to achieve it: we should use three classes you, butcher, farmer respectively refers to you, butchers, farmers uncle. One farmer uncle also provides a way to buy meat to the butcher call, this method input is the amount of money, return is the number of meat, with int, the code is as follows:

Copy Code code as follows:

Class Farmer {
public int buymeat (int) {
int meat = 0;
... meat = * * * *;
return meat;
}
}

And the butcher provides a way to buy meat for you to call, the same input money, return meat, but will be processed meat (killing pigs and shaving pig hair in the code in the province, or else have to write a class for pigs), the code is as follows:

Copy Code code as follows:

Class Butcher {
public int buymeat (int) {
Farmer farmer = new Farmer (); 1.find a farmer.
int meat = farmer.buymeat (money); 2.buy meat from the farmer.
Meat + 5; 3.inject 5 pound water into the meat and so weight would increase.
return meat; 4.return to you.
}
}

And the code that buys meat from the butcher's hand becomes this:

Copy Code code as follows:

Class You {
public void work () {
int Youmoney = 10;
Butcher butcher = new Butcher (); Find a butcher.
int meat = butcher.buymeat (Youmoney);
System.out.println ("Cook the meat, Weight:" + meat); You cooked it.
}
}

We can also optimize the program, we found that the butcher has a farmer has a same way to buy meat, we can extract an interface, called as a merchant (pedlar) bar, later you buy meat will not care whether he is a butcher or peasant uncle, as long as he has meat to sell on it, we extract an interface, the code becomes like this:

Copy Code code as follows:

Class You {
public void work () {
int Youmoney = 10;
Peldar peldar= New Butcher (); Find a Peldar.
int meat = peldar.buymeat (Youmoney);
System.out.println ("Cook the meat, Weight:" + meat); You cooked it.
}
}
Interface Peldar {
int buymeat (int money);
}
Class Butcher implements Peldar {
@Override
public int buymeat (int) {
Farmer farmer = new Farmer (); 1.find a farmer.
int meat = farmer.buymeat (money); 2.buy meat from the farmer.
Meat + 5; 3.inject 5 pound water into the meat and so weight would increase.
return meat; 4.return to you.
}
}

Class Farmer implements Peldar {
@Override
public int buymeat (int) {
int meat = 0;
... meat = * * * *;
return meat;
}
}

This is the proxy, and it is worth noting that the generic proxy class and the final class implement the same interface, and the advantage is that the caller does not have to care whether the current reference is a proxy or a final class.

But this is called static proxy, because the proxy class (butcher Class) is your own hand to write, dynamic agent is Java in the runtime, dynamic generation of an equivalent proxy class. Although the class is dynamically generated, the code to kill the pig and inject water is still to be written, just don't write a class. Where to write it, write it to the following interface:

Copy Code code as follows:

Public interface Invocationhandler {
public object invoke (object proxy, Method method, object[] args) throws Throwable;
}

What does the argument mean, I write like this, you may understand:

Copy Code code as follows:

Public interface Invocationhandler {
public object Invoke (Object butcher, Method Buymeat, object[] money) throws Throwable;
}

The first argument is an object of an automatically generated proxy class (an object that automatically generates a butcher class), the second parameter is the object of the method that is being invoked before (how do you still have an object, see the Java Reflection mechanism), we have only one method called Buymeat, so this parameter represents definitely it, The third argument is an array of arguments to the previous method, buymeat only one argument, so this array will have only one element. So the code to kill the pig's water is written in this way:

Copy Code code as follows:

Invocationhandler Minvocationhandler = new Invocationhandler () {
@Override
public object Invoke (Object butcher, Method Buymeat, object[] args) throws Throwable {
Farmer farmer = new Farmer (); 1.find a farmer.
int meat = (Integer) buymeat.invoke (farmer, args); 2.buy meat from the farmer.
Meat + 5; 3.inject 5 pound water into the meat and so weight would increase.
return meat; 4.return to you.
}
};

It's a little unorthodox to call the farmer's uncle's meat-buying method. Here is the reflex mechanism, which means that, to invoke the Buymeat method with the farmer object as the recipient, it is the same as the direct call to farmer, and you may ask why it is not called directly, you may not notice, The first parameter type of invoke is object, so you can publish the call command to any object (but not necessarily succeed, when it will succeed, etc.), if you have a lot of farmer objects, not even farmer objects, as long as an instance of an interface is available (which interface can be explained, We first name the A interface, we can pass it in as a parameter and then make a method call to it. Now let's look at how to generate the proxy class, which is simple enough to invoke the proxy's factory method as follows:

Copy Code code as follows:

public static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h)
Throws IllegalArgumentException

Explanation parameters, the first ClassLoader is used to load the proxy class (about ClassLoader, this article is not explained), you do not understand that it does not matter, the second is an array, each is an interface, the new generation of agents will implement all these interfaces, The method passed to Invocationhandler.invoke's second argument must belong to all the methods in these interfaces, and the A interface in the previous paragraph would have to be an element in the array, as the previous paragraph said the call failed. The third parameter Invocationhandler better understand that the Invocationhandler is notified whenever any method in the proxy class is invoked. Write the complete code below:

Copy Code code as follows:

Class You {
public void work () {
int Youmoney = 10;

Peldar peldarproxy = (Peldar) proxy.newproxyinstance (GetClass (). getClassLoader (), New Class[]{peldar.class}, Minvocationhandler);
int meat = peldarproxy.buymeat (Youmoney);

System.out.println ("Cook the meat, Weight:" + meat);
}

Invocationhandler Minvocationhandler = new Invocationhandler () {
@Override
public object Invoke (Object butcher, Method Buymeat, object[] args)
Throws Throwable {
Farmer farmer = new Farmer (); 1.find a farmer.
int meat = (Integer) buymeat.invoke (farmer, args); 2.buy meat from the farmer.
Meat + 5; 3.inject 5 pound water into the meat and so weight would increase.
return meat; 4.return to you.
}
};

}
Interface Peldar {
int buymeat (int money);
}

Class Farmer implements Peldar {
@Override
public int buymeat (int) {
int meat = 0;
... meat = * * * *;
return meat;
}
}

Here you generate a proxy class in the proxy class when the buymeat is called, the code is the same as the static agent before.

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.