From static proxy to dynamic proxy __string

Source: Internet
Author: User
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");

}

}

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.