The term proxy must be very familiar to everyone, because there are many contacts in reality. In fact, what in reality can reflect the abstract process and essence of the pattern very vividly and intuitively. Isn't the house so hot? Let's take the house as an example to reveal the veil of proxy.
Assume that you have a house to sell, one way is to directly publish the sales information online, and then directly bring the person who wants to buy the house to view the house, transfer the house, and so on until the house is sold, however, you may be very busy. You don't have time to deal with these things, so you can go to an intermediary and ask the intermediary to help you deal with these trivial things. The intermediary is actually your proxy. This is what you have to do. Now the intermediary helps you deal with them one by one. For the buyer, there is no difference between direct transaction with you and direct transaction with the same intermediary. The buyer may not even be aware of your existence, this is actually one of the biggest benefits of proxy.
Next, let's take a deeper look at why you need an intermediary instead of buying a house directly? In fact, a question exactly explains when the proxy mode should be used.
Cause 1: You may work in a field, and the person who buys the House cannot find you to trade directly.
Corresponding to our program design, the client cannot directly operate the actual object. So why can't I perform this operation directly? One case is that the object you want to call is on another machine, and you need to access it across the network. If you want to call it directly by using coding, you need to handle network connections, packaging, unpacking, and other complex steps. To simplify client processing, we use the proxy mode to create a remote object proxy on the client, the client calls the agent just like a local object and then connects the agent with the actual object. for the client, it may not feel that the called thing is on the other end of the network, this is actually how the Web Service works. In another case, although the object you want to call is local, you are afraid of affecting your normal operations because the call is very time-consuming. Therefore, you need to find a proxy to handle this time-consuming situation, the easiest thing to understand is that a large image is installed in the Word. When the word is opened, we must load the content to open it together, however, after loading the large image and opening the Word user, the system will be able to jump, so we can set a proxy for the image, let the proxy open the image slowly without affecting the function of opening the Word. I just guess that the Word is doing this. I don't know exactly how to do it.
Cause 2: You do not know how to transfer the ownership, or you need to do other things to achieve your goal.
Corresponding to our program design: In addition to the functions provided by the current class, we also need to add some other functions. The most easy to think of is permission filtering. I have a class for a business, but for security reasons, only some users can call this class, in this case, we can create a proxy class for this class, requiring all requests to pass through this proxy class for permission judgment. If it is secure, the actual service of this class will be called to start processing. Some people may say why I need to add more proxy classes? I only need to add permission filtering in the methods of the original class. Isn't that all done? In program design, there is a single-class principle. This principle is very simple, that is, every class has the function as single as possible. Why is it single, because only the class with a single function is less likely to be modified? For example, if you put the permission judgment in the current class, currently, this class is responsible for both its own business logic and permission judgment. There are two reasons for this change. If the permission rules change, this class must be changed. Obviously this is not a good design.
Well, the principle is almost done. If you talk about it again, you may have to throw bricks. Well, let's take a look at how to implement proxy.
Proxy mode implementation:
In fact, the proxy mode is very easy to implement. For example, you have a class responsible for returning employee salary information, as shown below:
Java code
- Public class BusinessClass
- {
- Public double GetPayroll (string employee)
- {
- // Return the salary result
- Return 1000;
- }
- }
- Because the salary information is confidential information of the company, and not anyone can call and view it, we provide a proxy for this class for user identity verification. The Code is as follows:
- Public class Proxy
- {
- Private BusinessClass bc;
- Public Proxy (BusinessClass bc)
- {
- This. bc = bc;
- }
- Public double GetPayroll (string user)
- {
- // Determine the user permission
- // If not, null is returned or an exception is thrown.
- If (IsManage (user ))
- {
- Return bc. GetPayroll ("Zhang San ");
- }
- Throw new Exception ("you do not have this permission .");
- }
- }
- Note: The proxy class must use the proxy class for business logic. Therefore, the proxy class must contain the instance of the proxy class, which is the same as the adapter mode.
- So far, the goal has been achieved, but in reality we often abstract a public interface for the proxy class and the proxy class, as shown below:
- Public interface IBusinessClass
- {
- Double GetPayroll (string user );
- }
Public class BusinessClass {public double GetPayroll (string employee) {// return the salary result return 1000;} because the salary information is confidential information of the company, no one can call to view it, therefore, we perform a Proxy for this class for user identity authentication. The Code is as follows: public class Proxy {private BusinessClass bc; public Proxy (BusinessClass bc) {this. bc = bc;} public double GetPayroll (string user) {// judge the user permission // if not, null is returned, or an exception is thrown if (IsManage (user) {return bc. getPayroll ("Zhang San");} throw new Exception ("you do not have this permission. ");}} Note: The proxy class must use the proxy class for business logic. Therefore, the proxy class must contain the instance of the proxy class, which is the same as the adapter mode. So far, the goal has been achieved, but in reality we often abstract a public interface for the proxy class and the proxy class, as follows: public interface IBusinessClass {double GetPayroll (string user );}
Many may ask why this interface needs to be abstracted? In fact, one of the biggest reasons for abstract interfaces is to constrain the behavior of both parties! What does it mean? In fact, I forced the Proxy to implement some methods, and these methods are the main business methods that are made public to the outside world. Of course, you can also rely on the programmer's self-discipline, but it is always good to have one more constraint. At least if we do not implement the specified method, we can find errors during the compilation period, which is better than the execution time. The other reason may be due to the benefits of using interfaces. I will not repeat them here. I will go through the interface article.