Proxy mode is easy to understand, is to replace someone else to do something, for example, we need to buy fruit, generally go to the supermarket or fruit shop to buy fruit, very few people go to the orchard to buy fruit, orchard is the place to produce fruit, but rarely sell fruit, here, the fruit shop, the supermarket has become an agent.
First, define an abstract class that provides all the function interfaces.
Define the abstract class selling fruit, that is, the interface, orchards and supermarkets to inherit this class.
#pragma onceclass csellfruits//defines an abstract class {public:csellfruits (void); virtual ~csellfruits (void); virtual void sellapple () = 0; Define interface, sell Apple virtual void Sellorange () =0;//define interface, sell oranges}; #include "SellFruits.h" csellfruits::csellfruits (void) {} Csellfruits::~csellfruits (void) {}
2. Define specific classes, that is, orchards, orchards produce fruit, but generally do not buy fruit
#pragma once#include "sellfruits.h" #include <stdio.h>class corchard:p ublic csellfruits{public:corchard (void); Virtual ~corchard (void); virtual void sellapple (); virtual void Sellorange ();}; #include "Orchard.h" Corchard::corchard (void) {}corchard::~corchard (void) {}void corchard::sellapple () {printf ("Sell Apple\n ");} void Corchard::sellorange () {printf ("Sell orange\n");}
3. Define the proxy class, the agent sells the fruit the class
#pragma once#include "sellfruits.h" #include "Orchard.h" #include <stdio.h>class cprocysellfruits:p ublic Csellfruits{public:cprocysellfruits (void); virtual ~cprocysellfruits (void); virtual void sellapple (); virtual void Sellorange ();p rivate:csellfruits *p_sellfruits; Incoming interface object}; #include "ProcySellFruits.h" cprocysellfruits::cprocysellfruits (void):p _sellfruits (NULL) {} Cprocysellfruits::~cprocysellfruits (void) {}void cprocysellfruits::sellapple () {if (this->p_sellfruits==null) { This->p_sellfruits=new Corchard (); Instantiate}this->p_sellfruits->sellapple () with the proxy class;//Agent Orchard sell Apple}void cprocysellfruits::sellorange () {if (this->p_ Sellfruits==null) {this->p_sellfruits=new corchard ();//Instantiate}this->p_sellfruits->sellorange () with the proxied class;// Agent Orchard sells Oranges}
4. Actual invocation
cproxysellfruits* p=new cproxysellfruits (); Sell Fruit p->sellapple with the agent class (); P->sellorange ();
Proxy mode of design mode (Agent mode)