当前位置:网站首页>代理模式——B站动力节点
代理模式——B站动力节点
2022-07-17 00:43:00 【KK-Greyson】
文章目录
001- 代理模式



002- 静态代理

2.1 静态代理的实现





如果有多个明星来表演,那么要使用面向接口编程:






2.2 静态代理的缺陷


003- 动态代理

3.1 JDK动态代理(掌握)

(1)JDK动态代理用到的类和接口


(2)实现JDK动态代理




package com.bjpowernode.proxy;
import com.bjpowernode.service.Service;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/** * */
public class ProxyFactory {
//类中的成员变量设计为接口,目标对象
Service target;
//传入目标对象
public ProxyFactory(Service target){
this.target = target;
}
//返回动态代理对象
public Object getAgent(){
return Proxy.newProxyInstance(
//ClassLoader loader, 类加载器,完成目标对象的加载
target.getClass().getClassLoader(),
//Class<?>[] interfaces,目标对象实现的所有接口
target.getClass().getInterfaces(),
//InvocationHandler h,实现代理功能的接口 ,我们传入的是匿名内部实现
new InvocationHandler() {
@Override
public Object invoke(
//创建代理对象
Object proxy,
//method就是目标方法sing(),show()
Method method,
//目标方法的参数
Object[] args) throws Throwable {
//代理功能
System.out.println("预订时间........");
//代理功能
System.out.println("预订场地........");
//主业务功能实现
//target.sing();还是写死了方法的调用, 不成
//sing(),show(),one()
Object obj = method.invoke(target,args);
//代理功能
System.out.println("结算费用........");
return obj; //切记:这个是目标方法的返回值
}
}
);
}
}








3.2 CGLib动态代理(了解)
(1)特点

(2)CGLib动态代理实现
简化版实现:



完成实现:




004- 总结

边栏推荐
- Rhce8 Learning Guide Chapter 1 installing rhel8.4
- [MCU simulation] (I) proteus8.9 installation tutorial
- 05_ Service call ribbon
- Mysql多表查询
- Multi table query - case exercise
- Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)
- A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?
- [single chip microcomputer simulation] (XII) instruction system logic operation instruction - logic XOR instruction XRL, accumulator clear 0 and reverse instruction
- 2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )
- [MCU simulation] (VII) addressing mode - bit addressing
猜你喜欢

Bisenetv2 face segmentation

Affine transformation implementation

Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)

Zabbix6.0 monitoring vcenter7.0

Rtx3090 installing pytorch3d
![[NoSQL] redis high availability and persistence](/img/73/53580db08b33c06c595fb3dfd6abce.png)
[NoSQL] redis high availability and persistence

04_ Service registration Eureka

Display zabbix6.0 information using grafana8.5.2

RESNET learning notes

Letv a plus de 400 employés? Le jour de l'immortel sans patron, les autorités ont répondu...
随机推荐
【单片机仿真】(七)寻址方式 — 位寻址
Fiddler grabbing
[MCU simulation] (VI) addressing mode - index addressing and relative addressing
This is a mathematical problem
【人脸识别】基于直方图Histogram实现人脸识别附matlab代码
[MCU simulation] (XXI) dB (define byte) - define byte
Pytorch best practices and code templates
Wechat applet -- Summary of problems in the actual development of taro framework
Wechat applet
Ncnn thread
[MCU simulation] (XIV) instruction system bit operation instructions - bit data transmission instructions MOV, bit variable modification instructions
Mysql多表查询
[MCU simulation] (VII) addressing mode - bit addressing
【单片机仿真】(六)寻址方式 — 变址寻址与相对寻址
Pure virtual function
Comparison between redis and other databases
Authentication code for wireless
Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)
Has DDD surpassed MVC
Powertor500t reports an error 0x01806803