当前位置:网站首页>Dynamic proxy
Dynamic proxy
2022-07-01 08:40:00 【SXHENIGA】
One 、 What is agency
sketch : Buying on behalf of 、 The mediation 、 ladder 、 Businesses and so on
characteristic : The agent and the target should do the same thing

Two 、 The proxy pattern
sketch : Agency mode means , Provides a proxy for other objects to control access to this object . In some cases , An object Not suitable or cannot be quoted directly Another object , The proxy object can be in Customer class and Target audience Play a role in The mediation The role of . In short : The target object is not accessible , Access... Through proxy object enhancements ;
effect : Enhancements 【 Add extra features 】; Control access to the target object ;
classification : Static proxy 【① The proxy class is implemented manually ② The target class needs to be certain 】; A dynamic proxy ;
3、 ... and 、 Static proxy
characteristic : The target object and the proxy object implement the same business interface ; The target object must implement the interface 【 Compared with dynamic proxy 】; The proxy object already exists before the program runs ; It can flexibly switch the target objects , But it can't be function Flexible handling of
shortcoming : In static proxy , Acting The function is dead , Unable to change dynamically , And this function already exists when the project starts ; If you want to add the function of agent , It needs to be in the interface 、 Target class and proxy class Add new code between , The impact is more , Dynamic proxy can automatically generate proxy classes .
Realization :
0、 Project directory
2、 Create factory class 【 Target class 】, Implementation steps 1 The interface of
3、 Create a merchant , It's the agent , You also need to implement steps 1 The interface of
4、 Create a client class , Call the merchant's method to buy something
|
Four 、 A dynamic proxy
sketch : Proxy objects are dynamically built in memory during program execution , It can be carried out flexibly Business function Of Switch . let me put it another way : When the program is running, the proxy class object JVM Generated dynamically according to the reflection mechanism , There is no need to define a proxy class .java object
1、JDK Dynamic proxy for :
sketch : Use java The classes and interfaces in the reflection package realize the function of dynamic agent
characteristic : The target object must implement the business interface ;JDK Proxy objects do not need to implement business interfaces ;JDK The object of dynamic proxy does not exist before the program runs , Dynamically build in memory while the program is running ;JDK Dynamic agent can flexibly switch business functions ; Methods in this class ( Methods in non interface ) Can't be represented ; You can enhance your functional code in the agent
Parameters : It is done using tool classes JDK Dynamic implementation of , Reflex package java.lang.reflect
![]() |
1)Proxy class : It is java.lang.reflect.Proxy Class under package , It has a way Proxy.newProxyInstance{....}, Specifically used to generate dynamic proxy objects
2)Method class : Class for reflection , Used to make reflection calls to target object methods ,Method Object to catch the method we are calling
3)InvocationHandler Interface : Used to implement agent and business functions , We use anonymous internal implementation when calling , We wrote it ourselves
Realization :
0、 Project structure
1、 Create an interface , Define the functions to be completed by the target class
2、 Create the implementation interface of the target class
3、 establish InvocationHandler Implementation class of interface , stay invoke Method to complete the function of the proxy class 1) Calling the target method 2) Enhancements
4、 Use Proxy Class static methods , Create proxy object , And convert the return value to the interface type .
Actually : Dynamic agents are better than static agents , Namely : Automatically add the enhanced code to the front or back of the method , You don't have to write enhanced code for every method ; But static proxy can only add enhanced code one method at a time , Although the enhanced code is the same . |
2、CGLib A dynamic proxy :
sketch : Referred to as subclass dynamic proxy , By dynamically building subclass objects in memory , Override the method of the parent class to enhance the proxy function . If the target object does not implement the interface , Only through CGLib Subclass proxy for function enhancement . Subclass proxies are object bytecode frameworks ASM To achieve ;CGLib stay MyBatis and Spring In the framework
Be careful : The represented class cannot be final, Because it's inheritance
边栏推荐
- Screenshot tips
- How to recruit Taobao anchor suitable for your own store
- Leetcode t31: next spread
- MAVROS发送自定义话题消息给PX4
- 你了解数据是如何存储的吗?(C整型和浮点型两类)
- MySQL8.0学习记录17 -Create Table
- vscode自定义各个区域的颜色
- Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
- SPL-安装与基本使用(二)
- DID的使用指南,原理
猜你喜欢

15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works

C basic knowledge review (Part 4 of 4)

Do you know how data is stored? (C integer and floating point)

factory type_ Id:: create process resolution

《微机原理》—总线及其形成

MAVROS发送自定义话题消息给PX4

Redis publish subscription

Glitch Free时钟切换技术

《单片机原理与应用》——并行IO口原理

Audio-AudioRecord create(一)
随机推荐
Advanced C language pointer (Part 2)
Introduction to R language
内存大小端
MATLAB【函数和图像】
factory type_id::create过程解析
yolov5训练可视化指标的含义
Huawei machine test questions column subscription Guide
C语言指针的进阶(下)
[JS reverse] MD5 encryption parameter cracking
为什么LTD独立站就是Web3.0网站!
明明设计的是高带宽,差点加工成开路?
Leetcode t40: combined sum II
MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
IT 技术电子书 收藏
MATLAB小技巧(23)矩阵分析--模拟退火
长安链同步节点配置与启动
Glitch Free时钟切换技术
Principle and application of single chip microcomputer - off chip development
一文纵览主流 NFT 市场平台版税、服务费设计
Matlab tips (23) matrix analysis -- simulated annealing
1、 Create an interface , Define the method of selling goods , Show what your manufacturers and businesses do .







