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