当前位置:网站首页>Agent model of structured model
Agent model of structured model
2022-06-22 19:34:00 【OldZhangYH】
Agency model of structural model
The proxy pattern
An object A Need to give an object B Provide a proxy to control the object A The interview of . At this time, access the object C Does not fit or cannot directly reference the target object , So you need a proxy object A As an access object C And accessed objects B The intermediary of .( Like buying a computer , We all bought them from the dealers in computer city , It is impossible to get goods directly from Asustek's factory ).
There are two modes of agency :
- Static proxy : The proxy class of the static proxy is generated during the compilation of the program .
- A dynamic proxy : The proxy class of dynamic proxy will be generated dynamically when the program is running .
structure
- Abstract theme class (Subject): Declare the business logic implemented by real topics and proxy objects through interfaces or abstract classes .
- Real topic class (Real Subject): Implements the methods of the abstract topic class , Is the real object represented by the proxy object , Is the object that is eventually referenced
- proxy class (Proxy): Provides the same interface as the real topic , There are internal references to real topics . You can visit 、 Control and extend the functionality of real topics
Static proxy
Asustek, a computer manufacturer, sells it to the dealer computer city at the ex factory price , Then the computer city expanded the selling function to increase the price 2000 Sell to the consumer .
A dynamic proxy
In the way of dynamic proxy , Proxy classes are generated automatically during the running process of a program . So we can't find a proxy class in the file Java Of documents . We used a ProxyFactory Class to generate the proxy class . it is to be noted that ProxyFactory Not a proxy class , Instead, it is a factory that produces proxy classes , A real proxy class will only be generated in memory during runtime .
JDK Provides Proxy Class newProxyInstance() Method to return the proxy object . This method has three parameters :
ClassLoader loader, Class loader for real topic classesClass<?>[] interfaces, The interface implemented by the class of the real topic classInvocationHandler, The method to be called by the proxy object
among InvocationHandler The implementation subclass of the interface represents the business logic of the proxy object , He needs to rewrite invoke Method also has three parameters :
proxyProxy object , Is the proxy object finally returned .methodReflected method objectsargsCall the parameters of the method
public class ProxyFactory {
private Asus asus = new Asus();
public ComputerOrz getProxyObj() {
ComputerOrz proxyObj = (ComputerOrz) Proxy.newProxyInstance(
asus.getClass().getClassLoader(),
asus.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
method.invoke(asus, args);
System.out.println(" Computer city markup 2000 Sell to consumers ( A dynamic proxy )");
return null;
}
});
return proxyObj;
}
}

The difference between dynamic agent and static agent
- Java All dynamic proxies are proxies for interfaces , That is to say, if you do not define an abstract topic class, you cannot use a dynamic proxy . Dynamic proxy centralizes all the methods in the interface into
invoke()in , In this way, it can be handled flexibly when there are many methods in the interface . The static proxy needs a method by method transfer . - When a method is added to the interface , Static proxy all implementation classes and proxy classes must implement this method . Dynamic proxy has no proxy class at all , We just need to modify it
invoke()Inside a part of the code can . - The proxy class of the static proxy is generated during the compilation of the program , The proxy class of dynamic proxy will be generated dynamically when the program is running .
Advantages and disadvantages
advantage :
- The proxy pattern acts as a mediator and protector between the client and the target object
- The proxy object can extend the function of the target object
- The proxy pattern separates the target object from the client , The coupling degree is reduced to some extent
shortcoming :
- The complexity of the system has increased
Use scenarios
- Remote agent
When connecting to a remote server locally , To realize network communication . But the network communication part 2 hidden , Only one interface is given to the local service , Don't care too much about the details of the communication part . - Firewall proxy
When the browser uses a proxy , The firewall will forward the browser's request to the Internet . Internet response , The proxy server is loading the corresponding to the browser - Protection agency
Control access to objects , Different levels of permissions can be provided to different users as required .
Source code
边栏推荐
- 将一维数据(序列)转化为二维数据(图像)的方法汇总GAFS, MTF, Recurrence plot,STFT
- what? Homekit, Micah, aqara and other ecosystems can also be linked with tmall elf ecology through zhiting?
- 线程池:ThreadPoolExcutor源码阅读
- canvas给图片画框框
- 5g short message solution
- 泡泡玛特:空洞的灵魂需要故事
- NLP-D57-nlp比赛D26&刷题D13&读论文&&找了一个多小时bug
- 贪心之分配问题(2)
- Implementing Domain Driven Design - using ABP framework - solution overview
- Detailed explanation of session mechanism and related applications of session
猜你喜欢

Weizhi technology appeared in the Western Digital Expo, and the space-time AI technology was highly recognized

C#,入门教程——关于函数参数ref的一点知识与源程序

Flutter series -flutter route management

远程访问及控制——SSH远程管理及TCP Wrappers 访问控制

Flutter系列-搭建Flutter开发环境

NLP-D57-nlp比赛D26&刷题D13&读论文&&找了一个多小时bug

知识蒸馏之Focal and Global Knowledge Distillation for Detectors

wpa_ State machine migration of supplicant

结构型模式之代理模式

修改antd tree组件,使其子类横向排列。
随机推荐
Weizhi technology appeared in the Western Digital Expo, and the space-time AI technology was highly recognized
Problems of different renderers running on the web with flutter2.0
Method of activity jump to fragment (intent)
Flutter series -flutter route management
Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“
远程访问及控制——SSH远程管理及TCP Wrappers 访问控制
AIOps 智能运维经验分享
使能伙伴,春节重大保障“不停歇”
Digital commerce cloud: analyze the design idea of B2B2C multi-user mall system architecture, and open a new era of intelligent mall
实践出真知:全网最强秒杀系统架构解密,不是所有的秒杀都是秒杀!!
将一维数据(序列)转化为二维数据(图像)的方法汇总GAFS, MTF, Recurrence plot,STFT
消息中间件(一)MQ详解及四大MQ比较
ssh免密码登录
贪心之区间问题(2)
shell脚本详解(七)——正则表达式、sort、uniq、tr
AUTOCAD——五种标注快捷键
session机制详解以及session的相关应用
2022 t elevator repair recurrent training question bank and answers
jniLibs. Srcdirs = ['LIBS'] what's the use?
Longest common subsequence