First, UML diagram
Second, the concept
Proxy Mode: provides a proxy for other objects to control access to this object.
Third, the application scenario
(1) A remote agent , which provides a local representation of an object in a different address space. This can hide the fact that an object exists in a different address space.
(2) virtual Agent , is to create expensive objects as needed. It is through him to store the real objects that take a long time to instantiate. For example: When a picture is loaded.
(3) A security agent that controls the permissions that are actually accessed by the object.
(4) Intelligent Guidance refers to the agent handling other things when the real object is called.
The fundamental principle: The proxy mode is actually the introduction of a certain degree of indirection when accessing the object, because this kind of indirection can be attached to a variety of purposes.
Iv. implementation of C + +
(1) Proxy.h
#include <iostream> #include <string>//schoolgirl class schoolgirl{private:std::string name;public:void SetName (std::string name) {this->name=name;} std::string GetName () {return name;}};/ The/subject class, which defines the common interface of the Realsubject and proxy, enables the use of Proxyclass igivegift{public:virtual void givedolls in any place where realsubject is used ( ) =0;virtual void giveflowers () =0;virtual void Givechocolate () =0;};/ /realsubject class, defines the real entity represented by proxy class Pursuit:public igivegift{private:schoolgirl* mm;public:pursuit (schoolgirl* mm) {this->mm=mm;} void Givedolls () {std::cout<<mm->getname () << "send you Doll" <<STD::ENDL;} void Giveflowers () {std::cout<<mm->getname () << "Send you Flowers" <<STD::ENDL;} void Givechocolate () {std::cout<<mm->getname () << "send you Chocolate" <<std::endl;}};/ The/proxy class, which holds a reference, is a proxy that can access the entity and provide an interface that is the same as the subject interface, so that the proxy can be used instead of the entity class Proxy:public igivegift{private:pursuit* GG; Public:proxy (schoolgirl* mm) {gg=new Pursuit (mm);} void Givedolls () {gg->givedolls ();} void Giveflowers () {Gg->givEflowers ();} void Givechocolate () {gg->givechocolate ();}};
(2) Main.cpp
#include "Proxy.h" #include <iostream> #include <stdlib.h>//client, client void Main () {schoolgirl* jiaojiao= New schoolgirl (); Jiaojiao->setname ("Jiao li Jiao"); proxy* daili=new Proxy (Jiaojiao);d aili->givedolls ();d aili->giveflowers ();d aili->givechocolate (); STD:: cout<< "" <<std::endl;system ("pause");
Big talk design pattern C + + Implementation-7th chapter-Proxy mode