Design pattern C + + implementation four: Proxy mode

Source: Internet
Author: User

Proxy mode: Provides a proxy for other objects to control access to this object.

Scenario: 1. A remote proxy, which provides a local representation of an object in a different address space, can hide the fact that an object exists in a different address space.

2. Virtual proxies create expensive objects as needed. It is the real object that takes a long time to store the instantiation. For example, the opening of a large HTML page, we see the picture is a download to see, and not open the picture frame, is the virtual agent to replace the real picture, at this time the agent stored the real picture path and size.

3. A security agent that controls access to real objects.

4. Smart reference refers to the fact that when the real object is called, the agent handles something else.


#ifndef proxy_h#define proxy_h#include<string> #include <iostream>using namespace Std;class schoolgirl{ Public:string name;//girl who is schoolgirl (string mm): Name (mm) {}};class igivegift{public:virtual void givedolls () const= 0; virtual void Giveflowers () const= 0;virtual void givechocolates () const = 0;}; Class Pursuit:p ublic igivegift{schoolgirl mm;//pursues who public:pursuit (schoolgirl m): mm (m) {}void givedolls () const; void Giveflowers () const;void givechocolates () const;}; Class Proxy:igivegift{pursuit gg;//for who proxy public:proxy (schoolgirl m): GG (Pursuit (m)) {}void givedolls () const{gg. Givedolls ();} void Giveflowers () const{gg. Giveflowers ();} void Givechocolates () const{gg. Givechocolates ();}}; void Pursuit::givedolls () const{cout << mm.name << "Give your Dolls.\n";} void Pursuit::giveflowers () const{cout << mm.name << "Give your Flowers.\n";} void Pursuit::givechocolates () const{cout << mm.name << "Give your Chocolates.\n";} #endif

#include "Proxy.h" int main () {schoolgirl m ("Summer"); Proxy Daili (m);//The operation of the agent here indicates that it was performed by Daili, and that the agent was initiated by pursuit GG, and that is, the flowers, dolls, chocolates are GG, just transferred by Daili to M. That is summerdaili.givechocolates ();d Aili. Givedolls ();d Aili. Giveflowers (); return 0;}


Design pattern C + + implementation four: Proxy mode

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.