当前位置:网站首页>"How to use" agent mode
"How to use" agent mode
2022-07-25 14:53:00 【Senior fishing Engineer】
This paper
Provide a placeholder or stand in for an object , To control access to this object ;
Use proxy mode to create proxy objects , The proxy object is used to access the proxy object . The proxied object can be a remote object 、 Objects that are expensive to create 、 Objects requiring security control .
Remote agent
Remote agent as another JVM Local representation of objects on . When calling a method on the proxy , The remote agent transmits the request to the remote through the network , And then transmit the results to the local through the network , And return it to the caller .Virtual agent
Virtual agents represent objects that are expensive to create . Virtual agents often wait until we really need an object , To create this object . Before the object is created 、 When creating , Access to this object is represented by virtual agents . After the object is created , The virtual agent will delegate the access of the object to the object it represents .- The caching proxy (Caching Proxy)
Provide temporary storage for expensive calculation results : It also allows multiple customers to share results , To reduce computing or network latency .
- The caching proxy (Caching Proxy)
Protection agency
According to different permissions , Control access to the proxied object .- Firewall proxy (Firewall Proxy)
Controls access to network resources , Protect “ The theme ” Free from “ Bad customer ” The effects of . - Intelligent reference agent (Smart Reference Proxy)
When the topic is referenced , Perform additional actions , For example, count the number of times an object is referenced .
- Firewall proxy (Firewall Proxy)
principle


Example

//subject
public interface PersonBean {
String getName();
}
//real subject
public class PersonBeanImpl implements PersonBean {
private String name;
@Override
public String getName() {
return name;
}
}
//handler
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class NotOwnerInvocationHandler implements InvocationHandler {
// hold project
private PersonBean personBean;
NotOwnerInvocationHandler(PersonBean personBean) {
this.personBean = personBean;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getIdCard".equals(method.getName())){
throw new IllegalArgumentException(" It's not your information , What do you look at? ");
}
return method.invoke(personBean,args);
}
}
import java.lang.reflect.Proxy;
public class ProxyUtil {
public static PersonBean getOwnerProxy(PersonBean personBean){
return (PersonBean) Proxy.newProxyInstance(
personBean.getClass().getClassLoader(),
personBean.getClass().getInterfaces(),
new OwnerInvocationHandler(personBean)
);
}
public static PersonBean getNotOwnerProxy(PersonBean personBean){
return (PersonBean) Proxy.newProxyInstance(
personBean.getClass().getClassLoader(),
personBean.getClass().getInterfaces(),
new NotOwnerInvocationHandler(personBean)
);
}
}
//test
private void drive(){
PersonBean tom = new PersonBeanImpl("tom");
PersonBean ownerProxy = ProxyUtil.getOwnerProxy(tom);
System.out.println("ownerProxy.getIdCard() = " + ownerProxy.getIdCard());
PersonBean notOwnerProxy = ProxyUtil.getNotOwnerProxy(tom);
try {
System.out.println("notOwnerProxy = " + notOwnerProxy.getIdCard());
} catch (Exception e) {
e.printStackTrace();
}
}
summary
- You think you use Object, But in fact, you use ProxyObject.“I am watching you , I can touch you .’”
- Proxy and InvocationHandler All in java.lang.reflect
- Proxy Create proxy object ,InvocationHandler You can decide whether to actually delegate the request to the proxy object .
Q&A
边栏推荐
- 优质数对的数目[位运算特点+抽象能力考察+分组快速统计]
- Add the jar package under lib directory to the project in idea
- Leetcode-198- house raiding
- GameFramework制作游戏(二)制作UI界面
- 冈萨雷斯 数字图像处理 第一章绪论
- MySQL 45 talks about | 06 global locks and table locks: Why are there so many obstacles to adding a field to a table?
- [C topic] the penultimate node in the Niuke linked list
- Awk from getting started to digging in (21) awk script debugging
- The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
- Thymeleaf setting disabled
猜你喜欢

27 选择器的分类

Gameframework making games (I)

微信公众号正式环境上线部署,第三方公众平台接入

冈萨雷斯 数字图像处理 第一章绪论

Wechat official account official environment online deployment, third-party public platform access

English语法_不定代词 - other / another

Resource not found: rgbd_launch 解决方案

45padding不会撑开盒子的情况

Niuke multi school E G J L

32 chrome调试工具的使用
随机推荐
Jqgrid select all cancel single line click cancel event
变分(Calculus of variations)的概念及运算规则
[C题目]牛客 链表中倒数第k个结点
各种平台dpkg包下载地址(包括arm64)
35 quick format code
32 chrome调试工具的使用
MySQL 45 talks about | 06 global locks and table locks: Why are there so many obstacles to adding a field to a table?
河源市区推出消防安全主题奶茶 助推夏季火灾防控
Browser based split screen reading
[eloquence] negotiation persuasion skills and Strategies
About RDBMS and non RDBMS [database system]
Writing standard of physical quantities and unit symbols
English grammar_ Indefinite pronoun - other / other
IP address classification, which determines whether a network segment is a subnet supernetwork
Leetcode-198- house raiding
27 classification of selectors
[C题目]力扣88. 合并两个有序数组
Alibaba cloud installs mysql5.7
BIO、NIO、AIO示例
Ten common application scenarios of redis