Let's talk about JAVA's proxy mode. I. Let's talk about java's proxy mode.
I. intermediary isolation
Proxy mode: The first time I came into contact with it, I read a book called "big talk design mode" when I was studying. Net. It has been almost three years since. I believe that all the students who have read this book remember the agency model in the book, which is the story of marrying others. Well, we will return to the definition of proxy mode: provide 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 proxy class has the same interface as the delegate class ,. Proxy mode is a common java design mode.
The format is as follows:
The previous figure is the original understanding of the proxy mode.
Ii. Open and Close principles, adding features,
Now we have a further understanding. The proxy class is not just an intermediary that isolates the client and the delegate class. We can also use the proxy to add some features without modifying the original code. This is a serious combination of Open and Close principles.
The proxy class is mainly responsible for pre-processing, filtering, forwarding, and post-processing messages for the delegate class. There is usually an association between the proxy class and the delegate class. A proxy class object is associated with a delegate class object. The proxy class object itself does not actually implement the service, but calls the relevant methods of the delegate class object, to provide specific services.
In this way, the real business functions are implemented by the delegate class, but some public services before the business class is implemented. For example, in project development, we have not added the buffer and log functions. If you want to add them later, you can use a proxy to implement the functions without opening the encapsulated delegate class.
Iii. Proxy Classification
Based on the above understanding of the proxy, we have different methods for the specific implementation of the proxy. According to the agent creation period, the proxy class can be divided into two types. : Static proxy and dynamic proxy.
Static Proxy: the source code is automatically generated by the programmer or a specific tool, and then compiled. Before running the program, the. class file of the proxy class already exists.
Dynamic Proxy: when the program is running, the reflection mechanism is used to dynamically create the process.
1. Static proxy
Let's take a look at the static proxy first. Three steps are required. First, we need to define the business interface and the business interface implementation class, then define the proxy class and implement the business interface. Finally, write a Client to call the interface.
First, you need to define business interfaces and business interface implementation classes.
<Span style = "font-family: KaiTi_GB2312; font-size: 18px; ">/*** define a business interface * @ author Cassie */public interface Account {// query public void queryAccount (); // modify public void updateAccount ();} <pre name = "code" class = "java">/*** interface implementation class (including business logic: delegate class * @ author Cassie */public class AccountImpl implements Account {@ Override public void queryAccount () {System. out. println ("query method... ") ;}@ Override public void updateAccount () {System. out. println ("modification method... ") ;}}</span>
Second: Define the proxy class to implement the business interface
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;">/*** proxy class (enhanced AccountImpl functions) * @ author Cassie */public class AccountProxy implements Account {private AccountImpl accountImpl;/*** rewrite the default constructor * @ param accountImpl: object */public AccountProxy (AccountImpl accountImpl) {this. accountImpl = accountImpl;} @ Override public void queryAccount () {System. out. println ("before business processing... "); // call the delegate class method. This is the specific business method account> Impl. queryCount (); System. out. println ("after business processing... ") ;}@ Override public void updateAccount () {System. out. println ("before business processing... "); // call the delegate class method; accountImpl. updateAccount (); System. out. println ("after business processing... ") ;}}</span>
Third: Write the client, the test class I wrote here.
<Span style = "font-family: KaiTi_GB2312; font-size: 18px; ">/*** test the Account class * @ author Cassie */public class TestAccount {public static void main (String [] args) {AccountImpl accountImpl = new AccountImpl (); // input the Business Object AccountProxy accountProxy = new AccountProxy (accountImpl) to be called here; // start to call the methods of the Business Object, both of which are enhanced. AccountProxy. updateAcc> ount (); accountProxy. queryAccount () ;}</span>
The execution result is as follows:
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> before business processing... </span>
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> modification method... </span>
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> after business processing... </span>
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> query method before business processing... </span>
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> after business processing... </span>
Now our static proxy is implemented, but the problem is also followed. Observe the code and you can find that each proxy class can only serve one interface, and one AccountProxy class implements one Account interface, if I have multiple interfaces, do I need to write multiple Proxy classes to match them. In this way, too many Proxies will inevitably be generated in program development. Besides calling different methods, all proxy operations are the same as other operations. At this time, the Code must be repeated. The best way to solve this problem is to use a proxy class to complete all the proxy functions, so we will introduce our dynamic proxy.