当前位置:网站首页>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
边栏推荐
- AttributeError: ‘KeyedVectors‘ object has no attribute ‘wv‘
- IPLOOK作为O-RAN联盟会员,将共同促进5G产业发展
- Cluster, distributed and microservice concepts and differences
- Play typical usage scenarios of kubernetes | dashboard for 5 minutes every day
- Service practice: use service to complete a download task
- IPLOOK 成为 RedHat(红帽)业务合作伙伴
- Flutter series - build a flutter development environment
- 实现领域驱动设计 - 使用ABP框架 - 解决方案概览
- Implementing Domain Driven Design - using ABP framework - solution overview
- Detailed explanation of session mechanism and related applications of session
猜你喜欢

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

YARN笔记

A homekit enabled camera? Zhiting IPC camera IC1 unpacking experience

5gc and satellite integrated communication scheme

IPLOOK作为O-RAN联盟会员,将共同促进5G产业发展

故障分析 | 从 data_free 异常说起

Detailed explanation of session mechanism and related applications of session

2022 t elevator repair recurrent training question bank and answers

Cluster, distributed and microservice concepts and differences

20billion vs 5billion, how much is the "dehydration" little red book worth?
随机推荐
antd tree 树状选择器 子类必填
IPLOOK作为O-RAN联盟会员,将共同促进5G产业发展
Some technical ideas:
ABAQUS 使用RSG绘制插件初体验
实验七 触发器
AUTOCAD——五种标注快捷键
如何提高工作效率?苹果电脑效率工具合集
数商云:解析B2B2C多用户商城系统架构设计思路,开启智能商城新时代
实验4 NoSQL和关系数据库的操作比较
Digital business cloud: build a digital supply chain system to enable enterprises to optimize and upgrade their logistics supply chain
如何更改Apple Watch上的表盘
数组对象根据id一 一对应填入(将arr1填入arr2中)
知识蒸馏之Focal and Global Knowledge Distillation for Detectors
Screw database document generator
shell脚本详解(十)——sed编辑器的使用方法
有效的括号
Shell script explanation (IV) -- while loop and until loop of loop statements (additional examples and analysis)
实现领域驱动设计 - 使用ABP框架 - 解决方案概览
Flush difficult to open an account? Is it safe to open an account online?
取zip包中的文件名