当前位置:网站首页>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
边栏推荐
- [Yu Yue education] Shandong Vocational College talking about railway reference materials
- 【MFC开发(17)】高级列表控件List Control
- 机动目标跟踪——当前统计模型(CS模型)扩展卡尔曼滤波/无迹卡尔曼滤波 matlab实现
- The data analyst will be ruined without project experience. These 8 project resources will not be taken away
- How can enterprises and developers take the lead in the outbreak of cloud native landing?
- The era of low threshold programmers is gone forever behind the sharp increase in the number of school recruitment for Internet companies
- [no title] free test questions for constructor municipal direction general foundation (constructor) and theoretical test for constructor municipal direction general foundation (constructor) in 2022
- 任务、线程、进程 区别
- Nacos - 服务发现
- 《微机原理》——微处理器内部及外部结构
猜你喜欢

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

Memory size end

5mo3 UHI HII HII 17mn4 19Mn6 executive standard

Gateway-88

Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting

《微机原理》-绪论

【华为机试真题详解】判断字符串子序列【2022 Q1 Q2 | 200分】

避免按钮重复点击的小工具bimianchongfu.queren()

The data analyst will be ruined without project experience. These 8 project resources will not be taken away

SPL-安装与基本使用(二)
随机推荐
Maneuvering target tracking -- current statistical model (CS model) extended Kalman filter / unscented Kalman filter matlab implementation
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
华为机试真题专栏订阅指引
Qt的模型与视图
Burpsuite -- brute force cracking of intruder
【js逆向】md5加密参数破解
任务、线程、进程 区别
It is designed with high bandwidth, which is almost processed into an open circuit?
Advanced C language pointer (Part 2)
DataBinding源码分析
Configuration and startup of Chang'an chain synchronization node
我想知道手机注册股票开户的流程?另外,手机开户安全么?
Leetcode t29: divide two numbers
Only in China! Alicloud container service enters the Forrester leader quadrant
Redis源码学习(29),压缩列表学习,ziplist.c(二)
Computer tips
Gateway-88
个人装修笔记
DID的使用指南,原理
串口转WIFI模块通信
1、 Create an interface , Define the method of selling goods , Show what your manufacturers and businesses do .







