Years have gone, began to work, time too fast, feel a false year ....
Proxy mode: Provides a proxy for other objects to control access to this object.
Role Division:Subject: Abstract role, declaring a common interface to real objects and proxy objects;Proxy: Agent role, access to proxy real objects,realsubject: Real role, the object that ultimately needs to be referenced.
The proxy mode is divided into four categories: remote agent, virtual agent, protection agent, smart reference .
The following notes, the main record virtual agent, about remote agents, protection agents, smart references, etc., you can check the information, the core idea is the same as its definition. In particular, the smart reference can be referred to the implementation of smart pointers, using reference counting, you can refer to: http://www.cnblogs.com/huiz/p/8313209.html
Virtual Proxy: You can cache additional information for an entity to delay access to it until we really need an object to create it .
For example: In the project, we often need to load resources, in the boot immediately load all the resources, will lead to some performance problems, such as the boot time is too long, there is need to need to load the resources when really need, we can define the proxy class, The resources of the real object are loaded in the proxy class so that lazy loading can be implemented. You can see the UML class diagram in detail:
And look at the code:
#include <iostream>using namespacestd;classBase//Subject{ Public: Virtual voidRequestloader () =0; Virtual~Base () {}};classControl: PublicBase//Realsubject{ Public: Virtual voidRequestloader () {cout<<"request Loader resources."<<Endl; }};classProxy: PublicBase//Proxy{Private: Control*M_con; Public: Proxy (Control*con): M_con (Con) {}Virtual voidRequestloader () {M_con-Requestloader (); } ~Proxy () {if(NULL! =M_con) { DeleteM_con; M_con=NULL; } }};intMainintargcChar**argv) {Control* Con =NewControl; Proxy* PR =NewProxy (con); PR-Requestloader (); DeletePR; PR=NULL; return 0;}
The output is as follows:
The above code on the VC6.0 run through, if there is a problem, also please advise, in this first thanked Ha.
Proxy mode (c + +)