Static proxy and dynamic proxy

Source: Internet
Author: User

The so-called proxy Design refers to the operation of a real subject by a proxy subject, and the actual subject executes specific business operations, while the proxy subject is responsible for the processing of other related businesses.

This is often the case in the system: verification is required before some operations, such as advanced user information verification when adding or deleting operations, to check whether the user has permissions for these operations. The Code is as follows:

// Usermanager interface: public interface usermanager {public void adduser (string username, string password); Public void deluser (INT userid);} // usermanagerimpl class to implement the usermanager interface: public class usermanagerimpl implements usermanager {public void adduser (string username, string password) {checksecurity (); system. out. println ("--------- usermanagerimpl. add () -------- ");} public void deluser (INT userid) {checksecurity (); system. out. println ("--------- usermanagerimpl. deluser () -------- ");} private void checksecurity () {system. out. println ("------- checksecurity -------");}}

When the verification function checksecurity () is added to the Add/delete method, the problem arises. If the verification function checksecurity () needs to be added to hundreds of functions, hundreds of checksecurity () are required () function. If the requirement changes one day and user verification is not required, you need to delete checksecurity () from hundreds of functions, resulting in poor maintainability. How can this problem be solved?

1. Static proxy

Add proxy class

Public class usermanagerimpl implements usermanager {public void adduser (string username, string password) {// checksecurity (); system. out. println ("--------- usermanagerimpl. add () -------- ");} public void deluser (INT userid) {// checksecurity (); system. out. println ("--------- usermanagerimpl. deluser () -------- ");} // Private void checksecurity () {// system. out. println ("------- checksecurity -------"); //} // usermanagerimplproxy proxy class: (Note: This proxy class can only serve usermanager APIs) public class usermanagerimplproxy implements usermanager {private usermanager; public usermanagerimplproxy (usermanager) {This. usermanager = usermanager;} public void adduser (string username, string password) {checksecurity (); usermanager. adduser (username, password);} public void deluser (INT userid) {checksecurity (); usermanager. deluser (userid);} private void checksecurity () {system. out. println ("------- checksecurity -------");}}

Static Proxy:

Proxy class -- created during compilation, usermanagerimplproxy can only serve the usermanager interface;


Adding or deleting a proxy class ensures that the target function is not changed and the maintainability is enhanced. However, the root problem is not solved because the problem is transferred to the proxy class, the problems mentioned above still exist. How can this problem be solved?

2. Dynamic Proxy:

Take out a total of checksecurity () Verification and place it in a separate class;

Proxy class-created at runtime, which can serve each interface;

Dynamic proxy considerations: place independent services (cross-cutting services) distributed in the system in one place and automatically place them at runtime. Horizontal issues are considered;

Code:

// Usermanager interface public interface usermanager {public void adduser (string username, string password); Public void deluser (INT userid );} // usermanagerimpl class implements the usermanager interface public class usermanagerimpl implements usermanager {public void adduser (string username, string password) {// checksecurity (); system. out. println ("--------- usermanagerimpl. add () -------- ");} public void deluser (INT userid) {// checksecurity (); system. out. println ("--------- usermanagerimpl. deluser () -------- ");} // Private void checksecurity () {// system. out. println ("------- checksecurity -------"); //} import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy; // The interface invocationhandlerpublic class securityhandler implements invocationhandler {private object targetobject; // target class, create proxy class based on the target // create proxy class public object createproxyinstance (Object targetobject) export this.tar GetObject = targetobject; // generate proxy based on the target // parameter: 1st parameters: Agent class loading proxy class; 2nd parameters: Target Interface; 3rd parameters: Return proxy. newproxyinstance (targetobject. getclass (). getclassloader (), targetobject. getclass (). getinterfaces (), this);} public object invoke (Object proxy, method, object [] ARGs) throws throwable {checksecurity (); // call the target method object ret = method. invoke (targetobject, argS); return ret;} private void checksecurity () {system. out. println ("------- checksecurity -------") ;}// client public class client {public static void main (string [] ARGs) {securityhandler hander = new securityhandler (); // generate the proxy usermanager = (usermanager) hander. createproxyinstance (New usermanagerimpl (); usermanager. adduser ("zhangsan", "123"); // call the dduser () method }}


3. Summary

Static proxy usually only acts as a proxy for one class. Dynamic proxy is used to represent multiple implementation classes under an interface.
The static proxy knows in advance what the proxy is, but the dynamic proxy does not know what the proxy is, and it is only known at runtime.
Dynamic proxy is the invoke method that implements the invocationhandler interface in JDK, but note that the proxy is an interface, that is, your service class must implement the interface, obtain the proxy object through newproxyinstance in proxy.
There is also a dynamic proxy cglib. The proxy is a class, and the business class inheritance interface is not required. The derived subclass is used to implement the proxy. You can modify the class by dynamically modifying the bytecode at runtime.


Static proxy and dynamic proxy

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.