Proxy (agent and dynamic agent) __java design pattern in Java

Source: Internet
Author: User
I. The concept of agency

Dynamic Proxy technology is the most important technology in the whole Java technology, it is the foundation of Learning Java Framework, do not dynamic agent technology, then learn spring these frameworks are not understand.

  Dynamic Proxy technology is used to generate a proxy object for an object . Why do you need to generate proxy objects for an object in development?
Give a real-life example: A singer or star has an agent of his own, the agent is their agent, when we need to find a star performance, we can not directly find the star, only to find the star agent. For example, Andy Lau in real life is very famous, can sing, dance, will be filming, Andy Lau in the absence of fame, we can directly find him singing, dancing, filming, Andy Lau famous, he did the first thing is to find a broker, this agent is Andy Lau's agent (agent), When we need to find Andy Lau performance, can not directly find Andy Lau (Andy Lau said, you find my agent to discuss specific matters!), only to find Andy Lau's agent, so the value of Andy Lau's presence is to intercept our direct visit to Andy Lau.
This real-world example is the same as we are in development, and the reason we have to produce an object's proxy object in development is primarily to intercept access to real business objects. So what should the proxy object have to do with it. The proxy object should have the same method as the target object

So here's a clear idea of the two concepts of proxy objects:
1, the value of the agent object is mainly used to intercept the real business object access .
2. the proxy object should have the same method as the target object (Real business object) . Andy Lau (Real business object) can sing, dance, will be filming, we can not directly find him singing, dancing, filming, can only find his agent ( agent ) singing, dancing, filming, a person to become Andy Lau's agent, Then he must have the same behavior as Andy Lau (singing, Can Dance, will be filming), Andy Lau has any method, he (agent) must have what method, we find Andy Lau's agent singing, dancing, filming, but the agent is not really know how to sing, dance, filming, really know how to sing, dance, filming is Andy Lau , the real example is that we want to find Andy Lau singing, dancing, filming, then can only find his agent, pay his agent, and then the agent let Andy Lau to sing, dance, filming. second, the proxy in Java 2.1, "java.lang.reflect.Proxy" Class introduction

  now, to generate a proxy object for an object, this proxy object will usually write a class to build , so first write the class that is used to build the proxy object. In Java How to use the program to generate an object's proxy object, Java after the JDK1.5 provides a "java.lang.reflect.Proxy" class, through the "Proxy" class provides a The Newproxyinstance method is used to create a proxy object for an object, as follows:

newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h) 

The newproxyinstance method is used to return a proxy object, which has a total of 3 parameters, and ClassLoader loader is used to indicate which class loader the build proxy object uses, class<?>[] Interfaces is used to indicate which object the proxy object is generated from, and Invocationhandler H is used to indicate what the generated proxy object is going to do with the interface designation. So we just need to call the newproxyinstance method to get a proxy object for an object. 2.2, write the class to generate proxy objects

  in Java, if you want to generate a proxy object for an object, then the object must have an interface , so the first step is to design the interface of the object and define the behavior (method) of the object in the interface.

1, define the object's behavior interface

1 package cn.gacl.proxy;
 2 
 3/**
 4 * @ClassName: Person
 5 * @Description: Defining the behavior of an object
 6 * @author: Aloof Wolf
 7 * @date: 2014-9-14 9:44 :
 8 *
 9 
/interface Person {     /**     * @Method: Sing
14     * @Description: Sing     * @Anthor: Proud and aloof Wolf
*     @param name     @ return     /*     Sing string (string name);     /**     * @Method: Sing     * @Description: Dancing     * @Anthor: Lonely Wolf     * @param name     @return
/*     Dance String (string name);
30}

  2. Define target business object class

1 package cn.gacl.proxy;
 2 
 3/**
 4 * @ClassName: Liudehua
 5 * @Description: Andy Lau realizes person interface, then Andy Lau will sing and dance
 6 * @author: Aloof Wolf
 7 * @d ate:2014-9-14 pm 9:22:24
 8 *
 9 * Public 
class Liudehua implements person {One
public     Str ing sing (String name) {         System.out.println "Andy Lau Sings" +name+ "song. ");         "The song is over, thank you." ";     the public     String Dance (string name) {         System.out.println (Andy Lau jumps "+name+" dance.) ");
Return         "Dance finished, thank you to the audience." ";
21}

3. Create proxy class for build proxy object

 1 package cn.gacl.proxy;
 2 3 Import Java.lang.reflect.InvocationHandler;
 4 Import Java.lang.reflect.Method;
 5 Import Java.lang.reflect.Proxy; 6 7/** 8 * @ClassName: Liudehuaproxy 9 * @Description: This agent is responsible for the formation of Andy Lau's agent * @author: Lonely Wolf One * @date: 2014-9-14 afternoon 9: 50:02 * * * public class Liudehuaproxy {15 16//Design a class variable remember the proxy class to proxy the target object to the private person LDH = new Liudeh
UA (); 18 19/** 20 * Design a method Generation proxy object * @Method: GetProxy @Description: This method returns Andy Lau's proxy object: Person person Liudehuaproxy.getproxy ()//Get a proxy object * @Anthor: Aloof Wolf * * @return An object's proxy object Son GetProxy () {28//using Proxy.newproxyinstance (ClassLoader loader, class<?>[) interfaces, Invocationhandler h) Returns the proxy object of an object Proxy.newproxyinstance (person), Liudehuaproxy.class getClassLoader (),       Ldh.getclass (). Getinterfaces (), new Invocationhandler () {32/** 33               * Invocationhandler interface only defines an invoke method, so for such an interface, we do not have to define a class to implement the interface, 34 * But directly using an anonymous inner class to real                      Now this interface, the new Invocationhandler () {} is the anonymous implementation class for Invocationhandler interface 35/36/** 37 * In the Invoke method encoding specifies the returned proxy object to do the work-proxy: Pass the agent's self in the Metho D: Transfer the current method of the proxy object into the args: Pass the method parameter in 41 * 42 * When calling the proxy object's                      Person.sing ("Ice Rain"); or person.dance ("Jiangnan style"); method, 43 * Actually executes the code in the Invoke method, 44                     * So we can use Method.getname () in the Invoke method to know which method of the proxy object is currently being invoked @Override 47 Public

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.