Proxy mode (c + +)

Source: Internet
Author: User

   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 + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.