[Java] 12: proxy mode-how to play with static proxy?
What is static proxy?
There is a Proxy Pattern in the 23 commonly used design patterns that are week-long ). It is defined as follows:
Provides a proxy for other objects to control access to this object. In some cases, an object is not suitable or cannot directly reference another object, and the proxy object can play a mediation role between the client and the target object.
The idea of proxy mode is to insert a proxy object between the actual object and the caller to provide additional processing or different operations. These additional operations usually need to communicate with the actual object.
So what is static proxy? Static means that the ing between the proxy class and the delegate class has been determined before the program runs, or the bytecode file of the proxy class before running has been created.
Why use proxy mode?
The proxy mode consists of three parts:
Abstract role: Declares the business method implemented by the role through an interface or abstract class.
Proxy role: implements an abstract role. It is the proxy of a real role. You can use the business logic method of the real role to implement the abstract method and attach your own operations.
Real role: Implements abstract roles and defines the business logic to be implemented by real roles for proxy roles to call.
For more information, see the following figure:
If we use the proxy mode to develop programs, we only need to focus on our own business logic. Other auxiliary functions such as logs and permission control do not need to be concerned, you can leave it to the proxy class. This ensures the high reusability of the business class.
How to use static proxy?
Here we use Java to write a simple demo to illustrate how to use static proxy. The general requirement is that the log printing function needs to be added to a user-managed business class. What should I do? Solution 1: directly write the log printing code to the method in the user-managed business logic implementation class; solution 2: use static proxy to add the log printing code to the proxy class method. Both methods are embodied in the sample code.
User Management Interface
public interface UserManager {public void addUser(String userId, String userName);public void delUser(String userId);public void modifyUser(String userId, String userName);public String findUser(String userId);}
User management implementation class
Public class UserManagerImpl implements UserManager {public void addUser (String userId, String userName) {// Add the log printing Code directly to the business implementation class method. // System. out. println ("start --> addUser () userId -->" + userId); try {System. out. println ("UserManagerImpl. addUser () userId --> "+ userId); // System. out. println ("success --> addUser ()");} catch (Exception e) {e. printStackTrace (); // System. out. println ("error --> addUser ()"); throw new RuntimeException () ;}} public void delUser (String userId) {System. out. println ("UserManagerImpl. delUser () userId --> "+ userId);} public String findUser (String userId) {System. out. println ("UserManagerImpl. findUser () userId --> "+ userId); return" query successful, found user Michael ";} public void modifyUser (String userId, String userName) {System. out. println ("UserManagerImpl. modifyUser () userId --> "+ userId );}}
User Management proxy
Public class UserManagerImplProxy implements UserManager {// Add a reference to the real object private UserManager userManager; // The proxy object is passed into the public UserManagerImplProxy (UserManager userManager) {this. userManager = userManager;} public void addUser (String userId, String userName) {try {// Add the log printing function for the core business method System. out. println ("start to add a user, call method: addUser () User ID: userId -->" + userId); userManager. addUser (userId, userName); System. ou T. println ("User Added successfully! ");} Catch (Exception e) {e. printStackTrace (); System. out. println (" failed to add user! Contact the administrator ...... ") ;}} Public void delUser (String userId) {userManager. delUser (userId);} public String findUser (String userId) {return userManager. findUser (userId);} public void modifyUser (String userId, String userName) {userManager. modifyUser (userId, userName );}}
Client
Public class Client {public static void main (String [] args) {// UserManager userManager = new UserManagerImpl (); UserManager userManager = new UserManagerImplProxy (new UserManagerImpl (); userManager. addUser ("0001", "Zhang San"); System. out. println (userManager. findUser ("0001 "));}}
Summary:
Static proxy has its advantages, but its disadvantages are also obvious. The proxy class in the static proxy implements the same interface as the delegate class, and the proxy class implements the same method through the delegate class. In this way, a lot of code repetition occurs. If a new method is added to the interface, except all the implementation classes need to implement this method, all the proxy classes also need to implement this method, which adds a lot of trouble to code maintenance.
Second, the static proxy mode serves only one type of objects. If we want to add a service class, we must add a proxy class accordingly. In this way, if our program is very large, using static proxy is a very cost-effective choice. How can this problem be solved? In addition, the next loop is decomposed.