In this paper, starting from purpose of moored floating column: segmentfault.com/blog/camile
This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details
version | The date of | note |
---|---|---|
1.0 | 2019.4.27 | The article first |
1.1 | 2021.5.21 | Modify the title:Rambling on about the agency model -> Talk about code: Rambling on about the agency model |
preface
The proxy pattern is a very common design pattern in programming. In the interview process, I often ask related questions, but many students’ answers are not satisfactory. In this article, the author wants to talk about the application and some practice of agent mode.
What
Let’s start with a picture
We can clearly see that the proxy and client are coupled, while the target is decoupled from the client.
Why
Loose coupling, mentioned above. In any design pattern, their purpose falls within the following range:
- Reduce code redundancy and improve code reuse
- Loose coupling
The proxy pattern can help us implement The Open Closed Principle.
Here we can give an example. Target may be a good programmer,client is a company. Throughout the recruitment process, if the Proxy is a headhunter, some headhunters may try to raise the price of the programmer. If the Proxy is Hr, it may come to kill the price. The programmer might go through the same process all the time:
- The electric plane
- To the surface
- Sign the contract
We can write different behaviors (special bargaining skills) in different proxies (HrProxy or headhunter Proxy), and our programmers just focus on the process.
How
Take Spring, the most commonly used framework in Java. Spring provides two main features:
- IOC(Inversion of Control)
- AOP(Aspect Oriented Programming)
As we know,Spring AOP is essentially done through the proxy pattern. Let’s take a closer look at the four types of AOP support that Spring provides:
- Proxy-based classic Spring AOP;
- Pure POJO section;
- @AspectJ annotation-driven facets;
- Injection AspectJ facets (available for Spring versions).
The first three are all variations of the implementation of Spring AOP, which is built on dynamic proxies, so Spring’s support for AOP is limited to method interception.
SpringAOP supports two patterns of dynamic proxies,JDK Proxy and cglib. When Spring discovers that the target is implemented as an interface by a Proxy class, it is implemented using the JDK Proxy.
- JDK Proxy is not done entirely by reflection, but also by ASM for bytecode manipulation. Essentially, it’s done through interface conventions
- Cglib is done entirely through ASM bytecode. The essence is realized through inheritance
The code might look like this:
Public class SpringAopTargetProxy extends Target{public void operate(){// Spring aop method1... super.operate(); //spring aop method2... }}Copy the code
AspectJ does this by compile-time weaving, which involves inserting code at compile time. So you can think of it as doing AOP based on static proxies.
Based on the above, we can also deduce that SpringAOP is not valid for finalorstatic methods.
What is the difference between call and execution?
- Call simply inserts code where this method is called
- Execution is inserting code before calling this method
A variation of the proxy pattern
Previously, we distinguished between static and dynamic proxies based on the timing of proxy generation. According to the way of use, there are two common types:
- Virtual Proxy: It generates and initializes instances only when they are really needed
- Remote Proxy: A Remote Proxy lets us not care whether the RealSubject role is on the network, but invoke its methods as if they were local methods. Remote Method Invocation (RMI) in Java is the equivalent of Remote Invocation.
Similar design patterns
Adapter
The Adapter pattern ADAPTS two objects that have different interfaces (apis) so that they work together. In Proxy mode, the Proxy role has the same interface (API) as the RealSubject role (transparency).
Decorator
The Decorator pattern is similar to the Proxy pattern in implementation (such as API consistency), but it is used for different purposes — the Decorator pattern is used to add new functionality. In the Proxy mode, compared with adding new functions, it pays more attention to reducing the workload by setting up agents.