1. Introduction
Proxy pattern is one of 23 common Java Design Patterns of gof. Definition of proxy mode: provide a surrogate or placeholder for another object to controlaccess
It (Provides a proxy for other objects to control access to this object.). Create a proxy object in proxy mode to allow the proxy object to control access to the target object (the target object can be a remote object, an overhead object can be created, or an object requiring security control ), in addition, some additional functions can be added without changing the target object.
2. UML class diagram
3. Roles and responsibilities in the Mode
Subject: Abstract topic role. An abstract topic class can be an abstract class or an interface. It is the most common business type definition and has no special requirements.
Realsubject: a topic role, also called a delegated role or a delegated role. Is the specific executor of the business logic.
Proxy: the proxy topic role, also called the delegate class and proxy class. It implements all the methods for defining abstract themes for specific topic roles, and performs preprocessing and aftercare before and after the specific topic roles are processed. (The simplest is printing logs)
4. Code Implementation
(1) Subject
PackageCom. Jackie. designpatterns. proxy;
/**
*
Abstract topic and define main functions
*/
PublicinterfaceSubject {
PublicvoidOperate ();
}
(2) realsubject
PackageCom. Jackie. designpatterns. proxy;
/**
*
Subject
*/
PublicclassRealsubject
ImplementsSubject {
@ Override
PublicvoidOperate (){
System.Out. Println ("realsubject operatestarted ......");
}
}
(3) proxy
PackageCom. Jackie. designpatterns. proxy;
/**
*
Proxy
*/
PublicclassProxy
ImplementsSubject {
PrivateSubject
Subject;
PublicProxy (subject ){
This. Subject = subject;
}
@ Override
PublicvoidOperate (){
System.Out. Println ("before operate ......");
Subject. Operate ();
System.Out. Println ("after operate ......");
}
}
(4) Client
PackageCom. Jackie. designpatterns. proxy;
/**
*
Customer
*/
PublicclassClient {
/**
*@ ParamARGs
*/
PublicstaticvoidMain (string [] ARGs ){
Subject subject =
NewRealsubject ();
Proxy proxy =NewProxy (subject );
Proxy. Operate ();
}
}
Running result:
Beforeoperate ......
Realsubject operate started ......
Afteroperate ......
5. application scenarios
In the real world, the secretary is equivalent to an agent and the boss has a meeting. In this case, the secretary can submit the meeting-related work, such as notifying the staff of the meeting time, arranging the venue, organizing the venue after the meeting, and so on, the boss only needs a meeting and does not need to do those things in person. Similarly, in our program design, we can also use the proxy mode to decouple the code that is composed of a series of irrelevant logic. For example, the log code in the Business Code can be carried out in the proxy. Spring AOP is a typical dynamic proxy application.
6. Proxy Mode Application Form
(1) remote proxy-you can hide the fact that an object exists in a different address space. The client can also access objects on a remote machine. The remote machine may have better computing performance and processing speed, and can quickly respond to and process client requests.
(2) virtual proxy-allows objects with high memory overhead to be created as needed. This object is created only when it is actually needed.
(3) copy-on-write proxy (copy-on-write proxy)-used to control object replication by delaying object replication until the customer really needs it. Is a variant of the virtual proxy.
(4) protection proxy-provide different levels of target object access permissions for different customers
(5) cache proxy-provides temporary storage for overhead computing results. It allows multiple customers to share results to reduce computing or network latency.
(6) Firewall proxy-controls access to network resources and protects the subject from malicious customer attacks.
(7) synchronizationproxy-Provides secure access to the topic in the case of multiple threads.
(8) Smart referenceproxy-provides some additional operations when an object is referenced, such as recording the number of calls to this object.
(9) Complexity hidingproxy: used to hide the complexity of a complex set of classes and perform access control. It is sometimes called the appearance proxy, which is not hard to understand. The complex hidden proxy and appearance mode are different because the proxy controls access, while the appearance mode is different because the proxy controls access, while the appearance mode only provides another set of interfaces.