why you use proxies.
If we were a team developer, a class of the team might not want to be exposed to other callers. At this point, some of the functionality of the class is needed, and the best way is to provide it to the caller in a proxy way. For a drop-off user, It just needs to know that the classes that are provided to it can execute a business logic. It is completely unnecessary to know exactly who the real executor is.
In addition, we can use the agent for the problem of cutting,
For example: In the user management class, there are two ways to add, delete, has been defined, but the new requirements, to the implementation of each method before the addition of a number of validation information, so that for each method to be modified, this time violates the opening and closing principle.
The code example is as follows:
Interface:
Public interface Usermanager {
public void AddUser (string userName, string password);
public void Deluser (String userId);
}
Implementation classes with no agents:
public class Usermanagerimp implements Usermanager {
public void AddUser (string userName, string password) {
Add a validation method before adding a method
CheckSecurity ();
System.out.println ("--------usermanager.adduser ()------");
}
public void Deluser (String userId) {
Add a validation method before deleting a method
CheckSecurity ();
System.out.println ("--------usermanager.deluser ()------");
}
The authentication method that needs to be added.
public void checksecurity () {
SYSTEM.OUT.PRINTLN ("-----checksecurity----");
}
}
To add a static proxy implementation class:
public class Usermanagerimplproxy implements Usermanager {
Private Usermanager Usermanager;
constructor, Di injection method
Public Usermanagerimplproxy (Usermanager Usermanager) {
This.usermanager = Usermanager;
}
public void AddUser (string username, string password) {
CheckSecurity ()//join validation
Usermanager.adduser (username, password); Execution method
}
public void Deluser (int userId) {
CheckSecurity ()//join validation
Usermanager.deluser (USERID);
}
The authentication method that needs to be added.
private void CheckSecurity () {
System.out.println ("-------checksecurity-------");
}
}
But the static proxy does not solve the real problem, that is, static proxy class still violates the opening and closing principle. So the dynamic agent appears:
Interface
Public interface Usermanager {
public void AddUser (string userName, string password);
public void Deluser (String userId);
}
Implementation class:
public class Usermanagerimp implements Usermanager {
public void AddUser (string userName, string password) {
System.out.println ("--------usermanager.adduser ()------");
}
public void Deluser (String userId) {
System.out.println ("--------usermanager.deluser ()------");
}
}
Dynamic Proxy Implementation Class
public class Securityhandler implements Invocationhandler {
Private Object TargetObject;
Build Agent Instance
public Object Createproxyinstance (object TargetObject) {
This.targetobject = TargetObject;
Return Proxy.newproxyinstance (Targetobject.getclass (). getClassLoader (),
Targetobject.getclass (). Getinterfaces (),
this);
}
Execution method
@Override
public object invoke (object proxy, Method method, object[] args)
Throws Throwable {
Validate before executing a method
CheckSecurity ();
Methods for executing the target class
Object rest = Method.invoke (targetobject, args);
return rest;
}
Methods of validation that need to be added
public void checksecurity () {
SYSTEM.OUT.PRINTLN ("-----checksecurity----");
}
}
Client:
public class Client {
public static void Main (string[] args) {
Instantiate proxy class
Securityhandler handler = new Securityhandler ();
Instantiate Usermanager by proxy class
Usermanager Usermanager = (usermanager) handler.createproxyinstance (New Usermanagerimp ());
Executes the Add method, which is actually performed in the Invoke method of the proxy class.
Usermanager.adduser ("Zhang San", "123");
}
}