Object proxy instance for Java Notes

Source: Internet
Author: User
The code is as follows: Copy code

Import java. lang. reflect. InvocationHandler;
Import java. lang. reflect. Method;
Import java. lang. reflect. Proxy;


// Interface to be implemented by the proxy
Interface IVehical {

// For example, I have written two interfaces here.
Void run ();
Void say ();
}

// Concrete implementation
Class Car implements IVehical {

// The following two methods are used as the implementation methods of the interface. If these methods are not available in the interface, a redundant method program will not be compiled here.
// Each call to these two methods triggers the invoke method in the proxy object.
Public void run (){
System. out. println ("Car is running ");
    }
   
Public void say ()
    {
System. out. println ("just one! ");
    }

}

// Proxy class
// This class is used to create a proxy object. It is simply encapsulated here.
Class VehicalProxy {

Private IVehical vehical;

Public VehicalProxy (IVehical vehical ){
This. vehical = vehical;
    }
// This method returns the created object proxy.
Public IVehical create (){
Final Class <?> [] Interfaces = new Class [] {IVehical. class };
Final VehicalInvacationHandler handler = new VehicalInvacationHandler (vehical );
   
Return (IVehical) Proxy. newProxyInstance (IVehical. class. getClassLoader (), interfaces, (InvocationHandler) handler );
    }
// This is a processor bound to the proxy object. Every time www.111cn.net calls a method in the object proxy, the invoke method in this processor is triggered.
Class VehicalInvacationHandler implements InvocationHandler {

Private final IVehical vehical;
   
Public VehicalInvacationHandler (IVehical vehical ){
This. vehical = vehical;
    }

// Every time the say or call of the object proxy is executed (as long as it is a method in the IVehical interface), the invoke will be executed
Public Object invoke (Object proxy, Method method, Object [] args)
Throws Throwable {

System. out. println ("-- before running ...");
Object ret = method. invoke (vehical, args );
System. out. println ("-- after running ...");
       
Return ret;
    }
   
    }
}

Class Main {
Public static void main (String [] args ){
   
IVehical car = new Car ();
VehicalProxy proxy = new VehicalProxy (car );
   
IVehical proxyObj = proxy. create ();
ProxyObj. say ();
    }
}

/*
* After the program runs, the following information will be printed. Therefore, we can see that the object proxy can add the included execution code to the method of a specific object,
* This function is very useful when we need to debug a log or bug, because we should not debug the code or
* The printed log code is written in the object, so that we can add the log code to the agent of the object, so as to compare the business with the program framework
* Function separation ensures the "pure" of the code ".
* Output:
* -- Before running...
* Car is running
* -- After running...
**/

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.