The concept of proxy: one role is used to replace another role to complete certain functions. I personally think it is very appropriate to cite the original words of a great god's understanding of proxy ).
The proxy must have three roles: 1. interface role; 2. A Role in the proxy object role concept; 3. The proxy object. Note: The proxy object and the proxy object must implement the same interface. I personally understand, don't you understand, right? Please correct it)
Static proxy is easy to understand. To put it bluntly, two implementation classes A and B implement the same interface. We can define class A as the proxy class, and Class B as the proxy class. Class A also adds some processing methods while implementing interfaces. These methods are processed before the method of the target object is called.
Interface:
Package spring. proxy;
Public interface UserManager
{
Public String getUser (int userID );
Public void delUser (int userID );
Public void modifyUser (String userID, String password );
}
Implementation Class B
Package spring. proxy;
Public class UserManagerImpl implements UserManager
{
@ Override
Public void delUser (int userID)
{
System. out. println ("delUser ------");
}
@ Override
Public String getUser (int userID)
{
System. out. println ("getUser ------");
Return "" + userID;
}
@ Override
Public void modifyUser (String userID, String password)
{
System. out. println ("modifyUser ------");
}
}
Implementation Class A proxy class)
Package spring. proxy;
Public class UserManagerImplProxy implements UserManager
{
Private UserManager userManager;
Public UserManagerImplProxy (UserManager userManager)
{
This. userManager = userManager;
}
@ Override
Public void delUser (int userID)
{
CheckMethod ();
UserManager. delUser (userID );
}
@ Override
Public String getUser (int userID)
{
CheckMethod ();
String user = userManager. getUser (userID );
Return user;
}
@ Override
Public void modifyUser (String userID, String password)
{
CheckMethod ();
UserManager. modifyUser (userID, password );
}
// Add a Processing Method
Public void checkMethod ()
{
System. out. println ("checkMethod -------");
}
}
Client Testing
Package spring. proxy;
Public class Client
{
Public static void main (String [] args)
{
UserManagerImplProxy proxy = new UserManagerImplProxy (new UserManagerImpl ());
Proxy. delUser (1 );
}
}
Test results:
CheckMethod -------
DelUser ------
Dynamic Proxy:
A jdk interface is required for dynamic Proxy: java. lang. reflect. InvocationHandler
Create a class to implement this interface,
Package spring. proxy;
Import java. lang. reflect. InvocationHandler;
Import java. lang. reflect. Method;
Import java. lang. reflect. Proxy;
Public class DynamicProxy implements InvocationHandler
{
Private Object targetObject;
// Create a proxy based on the target object
Public Object createProxyInstance (Object targetObject)
{
This.tar getObject = targetObject;
Return Proxy. newProxyInstance (targetObject. getClass (). getClassLoader (),
TargetObject. getClass (). getInterfaces (), this );
}
@ Override
Public Object invoke (Object proxy, Method method, Object [] args)
Throws Throwable
{
CheckMethod ();
Object obj = method. invoke (targetObject, args );
Return obj;
}
// New Processing Function
Public void checkMethod ()
{
System. out. println ("checkMethod -------");
}
}
Test class:
Package spring. proxy;
Public class Client
{
Public static void main (String [] args)
{
DynamicProxy dynamicProxy = new DynamicProxy ();
UserManager userManager = (UserManager) dynamicProxy. createProxyInstance (new UserManagerImpl (); // The UserManagerImpl class is the implementation Class A of the static proxy, and the Implementation interface UserManager
UserManager. getUser (1 );
}
}
Test results:
CheckMethod -------
GetUser ------