Definition of proxy mode:
Provide a surrogate or placeholder for another object to control access to it.
Provides a proxy for other objects to control access to this object.
Some excerpted comments:
Proxy mode uses proxy objects to complete user requests and shield users from accessing real objects . A real-world agent is authorized to perform some of the client's affairs without the client's presence, and from the perspective of a third party, it appears that the client does not exist because he only communicates with the agent. In fact, the agent needs the authorization of the parties, and also needs to consult the parties on the core issues.
In software design, there are many intentions of using the proxy mode, such as the need to shield the client from directly accessing the real object for security reasons , or the need to use proxy classes in remote calls to handle the technical details of remote method calls (such as RMI) , or for Improve system performance and encapsulate real objects to achieve the purpose of lazy loading .
General class diagram of proxy mode:
Agent mode roles are divided into 4 types:
- Theme interface : define the public external methods of the proxy class and the real subject, and it is also the method for the proxy class to proxy the real subject;
- Real subject : the class that actually implements the business logic;
- Proxy class : used to proxy and encapsulate real themes;
- Client : The client, which does some work using the proxy class and the subject interface.
theme interface
1 package cn.rocker.designpattern.proxy; 2 3 /** 4 * @author rocker 5 * @version V1.0 6 * @Description: abstract theme class 7 * @date 2018/5/6 18:55 8 */ 9 public interface Subject { 10 // Define a method 11 void request(); 12 }
real subject
1 package cn.rocker.designpattern.proxy; 2 3 /** 4 * @author rocker 5 * @version V1.0 6 * @Description: 7 * @date 2018/5/6 19:00 8 */ 9 public class RealRequest implements Subject{ 10 @Override 11 // Implementation method 12 public void request() { 13 // Business processing logic 14 System.out.println("i am the real object..." ); 15 } 16 }
proxy class
1 package cn.rocker.designpattern.proxy; 2 3 /** 4 * @author rocker 5 * @version V1.0 6 * @Description: 7 * @date 2018/5/6 19:01 8 */ 9 public class Proxy implements Subject{ 10 11 private Subject subject = null; 12 public Proxy(Subject subject){ 13 this.subject = subject; 14 } 15 16 @Override 17 public void request() { 18 this.before(); 19 this.subject.request(); 20 this.after(); 21 } 22 23 private void before() { 24 //前处理 25 System.out.println("i am the preHandler of the object..."); 26 } 27 28 private void after() { 29 //后处理 30 System.out.println("i am the postHandler of the object..."); 31 } 32 }
client
1 package cn.rocker.designpattern.proxy; 2 3 import org.junit.Test; 4 5 /** 6 * @author rocker 7 * @version V1.0 8 * @Description: 9 * @date 2018/5/6 19:15 10 */ 11 public class Client { 12 @Test 13 public void test(){ 14 RealRequest realRequest = new RealRequest(); 15 Subject proxy = new Proxy(realRequest); 16 proxy.request(); 17 } 18 }