当前位置:网站首页>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
边栏推荐
- 3、Modbus通讯协议详解
- Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
- [deep analysis of C language] - data storage in memory
- Yolov5 advanced 7 target tracking latest environment setup
- 公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度
- I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
- Luogu p1088 [noip2004 popularization group] Martians
- factory type_ Id:: create process resolution
- 2022.2.15
- Leetcode t34: find the first and last positions of elements in a sorted array
猜你喜欢

Only in China! Alicloud container service enters the Forrester leader quadrant

华为机试真题专栏订阅指引

vscode自定义各个区域的颜色

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

Introduction to R language

What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR

factory type_id::create过程解析

Pipeline detection of UAV Based on gazebo

Burpsuite -- brute force cracking of intruder

Share 7 books I read in the first half of 2022
随机推荐
嵌入式工程师常见面试题2-MCU_STM32
【华为机试真题详解】判断字符串子序列【2022 Q1 Q2 | 200分】
《单片机原理及应用》—定时器、串行通信和中断系统
Burpsuite -- brute force cracking of intruder
MD文档中插入数学公式,Typora中插入数学公式
Redis publish subscription
15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works
任务、线程、进程 区别
yolov5训练可视化指标的含义
C语言指针的进阶(上篇)
FreeRTOS学习简易笔记
Only in China! Alicloud container service enters the Forrester leader quadrant
[untitled]
Do you know how data is stored? (C integer and floating point)
为什么LTD独立站就是Web3.0网站!
How to recruit Taobao anchor suitable for your own store
Nacos - 服务发现
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
Configuration and startup of Chang'an chain synchronization node
1.jetson与摄像头的对接
1、 Create an interface , Define the method of selling goods , Show what your manufacturers and businesses do .







