当前位置:网站首页>Principe de réalisation de l'agent dynamique
Principe de réalisation de l'agent dynamique
2022-06-29 23:13:00 【Invincible boy blogger】
Agent dynamique
Le proxy dynamique provient du modèle proxy dans le modèle de conception,Ici“Dynamique”L'auteur l'interprète comme plus flexible,Définir une interface dans une classe de proxy dynamique pour faire(InvocationHandler)Variable membre,Et appeler la méthode définie par l'interface(invoke),Méthode de définition de l'interface(invoke)Ne pas le réaliser,Au lieu de cela, laissez cette classe d'implémentation d'interface(Utiliser icilambdaMise en œuvre de l'expression)Pour le réaliser.Pour obtenir un effet dynamique.
FooInterface
interface Foo{
void foo();
void bar();
}
FooClasse d'implémentation de l'interfaceTarget
class Target implements Foo{
@Override
public void foo() {
System.out.println("foo()");
}
@Override
public void bar() {
System.out.println("bar()");
}
}
Interface proxy
// Interface proxy
interface InvocationHandler{
void invoke(Method method,Object[] objects) throws Throwable;
}
Agents
public class $Proxy0 implements Foo {
/** * UtiliserInvocationHandlerInterface, Transfert d'une partie de la mise en oeuvre d'une méthode nécessitant un mandataire à sa mise en oeuvre de sous - classe , * .Une seule interface est nécessaire dans la classe proxy , Et appelé dans la méthode qui nécessite un mandataire invokeLa méthode est juste. */
private InvocationHandler h;
public $Proxy0(InvocationHandler h) {
this.h = h;
}
@Override
public void foo() {
try {
Method foo = Foo.class.getDeclaredMethod("foo");
h.invoke(foo,new Object[0]);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void bar() {
try {
Method bar = Foo.class.getDeclaredMethod("bar");
h.invoke(bar,new Object[0]);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
mainClasse de méthode
public class Main{
public static void main(String[] args) {
$Proxy0 proxy = new $Proxy0(new InvocationHandler() {
@Override
public void invoke(Method method, Object[] objects) throws Throwable {
// 1、Améliorations fonctionnelles
System.out.println("before....");
// 2、Méthode originale
Object result = method.invoke(new Target(), objects);
}
});
proxy.foo();
proxy.bar();
}
}
边栏推荐
- 股票开户安全吗?上海股票开户。
- grpc的开发详解
- flutter 插件版本冲突的解决方法
- Phpspreadsheet reading and writing Excel files
- Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet
- The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]
- Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
- Can you be a coder if you don't learn English well? Stop worrying and learn it first
- 稳!上千微服务接入 Zadig 的最佳姿势(Helm Chart 篇)
- Cloud native enthusiast weekly: cool collection of grafana monitoring panels
猜你喜欢

Wechat applet: (update) cloud development wechat group contacts

sql刷题595. 大的国家

《天天数学》连载54:二月二十三日

Discussion on distributed unique ID generation scheme

Touch key and key control corresponding LED status reversal

语音信号处理(三):语音信号分析【连续的“模拟信号”--采样、量化、编码-->离散的“数字信号”】
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet

error: C2665: “QMessageBox::critical”: 4 个重载中没有一个可以转换所有参数类型

Qdomdocument and qdomnode are used in QT to read XML

微博系统中”微博评论“的高性能高可用计算架构
随机推荐
Gnawing down the big bone - sorting (I)
架构实战营模块 5 作业
Day9 ---- 用户注册与登录
在线文本数字识别列表求和工具
基金的利润分配与税收
Intranet penetration (NC)
Ansible automatic operation and maintenance
STM32 basic knowledge points
How tcpdump filters specific TCP flag bits
Status acquisition and control system of on-site express cabinet
从零实现深度学习框架——LSTM从理论到实战【理论】
Mysql database: use the show profile command to analyze performance
Open source the Ernie tiny lightweight technology of "Wenxin big model", which is accurate and fast, with full effect
Does rapid software delivery really need to be at the cost of security?
STM32基础知识点
What if MySQL fails to store emoticons
模板函数与特化函数实现高效dynamicCast
缓冲流练习
Wireshark data analysis and forensics information pacapng
Touch key and key control corresponding LED status reversal