Big talk design pattern C + + Implementation-7th chapter-Proxy mode

Source: Internet
Author: User

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" &LT;&LT;STD::ENDL;} void Giveflowers () {std::cout<<mm->getname () << "Send you Flowers" &LT;&LT;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

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.