当前位置:网站首页>Proxy mode (proxy)
Proxy mode (proxy)
2022-06-28 09:39:00 【Beginner Xiaobai Lu】

Provides a proxy for other objects to control access to this object . This enables the separation of business and core functions .
Mainly solve the problem of direct access to objects , for instance : The object to be accessed is on a remote machine . In an object-oriented system , Some objects for some reason ( For example, object creation costs a lot , Or some operations need safety control , Or you need out of process access ), Direct access will bring a lot of troubles to users or system structure , We can add an access layer to this object when we access it .
Use time : Want to do some control when accessing a class .
solve : Add middle layer .
** Key code :** Implementation and proxy class composition .
Application example :
1、Windows Shortcuts inside .
2、 Zhu Bajie went to find Gao Cuilan, and it turned out that monkey king had changed , It can be understood in this way : Abstract the appearance of Gao Cuilan , Gao Cuilan and Sun Wukong have realized this interface , When Zhu Bajie visited Gao Cuilan, he couldn't see that this was Monkey King , So Sun Wukong is the agent of Gao Cuilan .
3、 You don't have to buy a train ticket at the railway station , You can also go to the agency .
4、 A check or certificate of deposit is the agent of funds in an account . Checks are used in place of cash in market transactions , And provide control of funds on the account number of the issuer .
5、spring aop.
form
- Subject( Abstract theme role ): Declare the common interface between the real theme and the proxy theme , This allows you to use a proxy theme anywhere you use a real theme , Clients typically need to program for abstract topic roles .
- Proxy( Surrogate subject role ): The proxy subject role refers to the real subject role through the association relationship , Therefore, you can control and manipulate real subject objects ; The proxy theme role provides the same interface as the real theme role ( In order to replace the real theme role in the era of need ), At the same time, new services and functions can be added before or after calling the operation on the real subject object ;
- RealSubject( Real thematic role ): It defines the real object represented by the agent role , Real business operations are implemented in real theme roles , The client can indirectly invoke the operations defined in the real topic role through the proxy topic role .
- client (Client) It can interact with services or agents through the same interface , So you can use proxies in all the code that needs service objects .
advantage
1、 Clear responsibilities . The real role is to implement the actual business logic , Don't care about other matters that are not the responsibility , Complete a transaction through a later agent , The accompanying result is simple and clear programming .
2、 High scalability .
3、 Intelligent . The proxy object ACTS as a mediator between the client and the target object , In this way, it plays an intermediary role and protects the target object .
shortcoming :
1、 Due to the addition of proxy objects between the client and the real theme , As a result, some types of proxy patterns may slow down the processing of requests .
2、 Implementing the proxy pattern requires additional work , Some proxy patterns are very complex to implement .
classification
- Virtual agent (Virtual Proxy): It's creating expensive objects on demand , It is used to store real objects that need a long time to be instantiated , Make it created only when it's really needed .
Example , The main purpose of virtual agents is to achieve latency , A document editor that can embed graphic objects in documents . Some drawing objects are expensive to create . But opening documents must be fast , Therefore, when opening documents, we should avoid creating all expensive objects at once . Here you can use the agent model , When opening a document , Drawing objects are not opened , Instead, open the proxy of the drawing object to replace the real drawing . When you really need to open the drawing , The agent is still responsible for opening .
// abstract class
class Image
{
public :
Image(std::string name) : m_name(name){
}
virtual ~Image( ){
}
virtual void Show( ) = 0; // Functions that display documents
protected :
std::string m_name; // Document name
};
// Large entity class
class BigImage : public Image
{
public :
BigImage(std::string name) : Image(name){
}
virtual ~BigImage( ){
}
void Show( )
{
std::cout <<"This is Big Image..." <<std::endl;
}
};
// Large image proxies
class BigImageProxy : public Image
{
public :
BigImageProxy(std::string name) :Image(name), m_bigImage(NULL){
}
virtual ~BigImageProxy( )
{
delete m_bigImage;
}
void Show( )
{
if(this->m_bigImage == NULL)
{
m_bigImage = new BigImage(this->m_name);
}
m_bigImage->Show( );
}
private :
BigImage *m_bigImage;
};
// Client code
int main( )
{
Image *Image = new BigImageProxy("Image.txt");
Image->Show( );
delete Image;
return 0;
}
- Remote agent (Remote Proxy): Provide local representation of an object in different address spaces , This hides the fact that an object exists in different address spaces . This different address space can be on this machine , Or in another machine . More common in communications , Such as network 、IPC and RPC in , Generally, it is necessary to provide customer auxiliary objects and service auxiliary objects , Create the same method for the customer helper object as the service object ( Such as JAVA Medium stub and skeleton), And then realize the specific communication between them , about client for stub( It's actually stub and skeleton Work together ) Is its remote proxy .
- Intelligent reference agent (Smart Reference Proxy): When a real object is called , Agents deal with other things , For example, record the number of calls to this object .
Security agent : Also known as protection agent , Used to control the access rights of real objects , If necessary , Different permissions can be provided to different callers . - Copy on write agent : A kind of virtual agent , Postpone replication until the customer needs it .
- The caching proxy (Cache Proxy): Provide temporary storage space for the operation results of a target , So that other customers can share and access , A bit of a cache .
- Firewall proxy : The object of protection , Don't let users access , Special case of security agent .
- Synchronous agent : It allows several users to access the same object at the same time without conflict .
- Protection agency (Protection Proxy)
According to the access rights , You can use protection agents to control access to resources . for example , There is an employee object , The protection agent can allow ordinary employees to call some methods of the object , The administrator calls other methods .
matters needing attention :
1、 Difference from adapter mode : The adapter pattern primarily changes the interface of the object under consideration , However, the proxy mode cannot change the interface of the proxy class .
2、 Difference from decorator mode : Decorator mode for enhancement , And the agent model is to control it .
边栏推荐
- Test cases for learning the basic content of software testing (II)
- Two interview demo
- Differences between task parameter types inout and ref
- 浅谈小程序对传媒行业数字化的影响
- 静态代码块永远先执行? 格局小了!!!
- PMP Exam key summary IX - closing
- 如何查看谷歌浏览器保存的网页密码
- Stock suspension
- Write a simple timeline
- Multithreading concurrent parallel threaded process
猜你喜欢

The digital human industry is about to break out. What is the market pattern?

Redis5.0 slot migration, free play (single machine migration cluster)

Calculation of stock purchase and sale expenses

代理模式(Proxy)

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"

JVM family (2) - garbage collection

线程的生命周期

P2394 yyy loves Chemistry I

Unity AssetBundle资源打包与资源加载

Boundary value analysis method for learning basic content of software testing (2)
随机推荐
Threads and processes
Full link service tracking implementation scheme
Basic knowledge of hard disk (head, track, sector, cylinder)
Decision table method for basic content learning of software testing (2)
全链路业务追踪落地实践方案
PMP考试重点总结七——监控过程组(1)
abnormal
June 27, 2022: give a 01 string with a length of N. now please find two intervals so that the number of 1 is equal and the number of 0 is equal in the two intervals. The two intervals can intersect bu
Fastjason filter field
PMP考试重点总结八——监控过程组(2)
Calculation of stock purchase and sale expenses
自定义异常类及练习
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
函数的分文件编写
Virtual machine 14 installing win7 (Figure tutorial)
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠
JVM系列(2)——垃圾回收
PMP examination key summary VIII - monitoring process group (2)
MySQL基础知识点总结
静态代码块永远先执行? 格局小了!!!