当前位置:网站首页>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();
}
}
边栏推荐
- redis客户端
- MySQL backup database Linux
- 模板函数与特化函数实现高效dynamicCast
- Intranet penetration (NC)
- 3D stereo photo album, Valentine's day, couple birthday gift code applicable
- 剑指 Offer 38. 字符串的排列
- 0. grpc environment setup
- Mysql database: partition
- 把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
- Hematemesis finishing: a rare map of architects!
猜你喜欢
Design of Distributed Message Oriented Middleware
5 - 1 Analyse de vulnérabilité du système
math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)
Constexpr function
Free PDF to word software sharing, these software must know!
疫情下我离职一年,收入增长了10倍
Processing of error b6267342 reported by AIX small machine in production environment
Summer rainbow comes for dinner
Realizing deep learning framework from zero -- LSTM from theory to practice [theory]
Realizing deep learning framework from zero -- RNN from theory to practice [practice]
随机推荐
constexpr 函数
从零实现深度学习框架——RNN从理论到实战【实战】
Uniapp copy contents to clipboard
Does rapid software delivery really need to be at the cost of security?
把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
字节云数据库未来方向的探索与实践
Digital tracking analysis of insurance services in the first quarter of 2022
利用kubernetes中的leader选举机制来完成自己的HA应用
Welcome the "top ten" of the Municipal Association for science and technology • pay tribute to Lu Yi, a scientific and technological worker: an explorer guarding the transmission security of the power
Arrange the array into the smallest number_ Reverse pairs in an array (merge Statistics)_ Number of occurrences of a number in an ascending array_ Ugly number (Sword finger offer)
mysql备份数据库linux
优雅的改造短信业务模块,策略模式走起!
Node data collection and remote flooding transmission of label information
[php8+oracle11g+windows environment without tools] Intranet / no network /win10/php connecting to Oracle database instance
股票开户安全吗?上海股票开户。
[cooking record] - hot and sour cabbage
Weekly Postgres world news 2022w25
Daily mathematics serial 54: February 23
Mysql database: the difference between drop, truncate and delete
微博系统中”微博评论“的高性能高可用计算架构