Proxy mode of design mode (Agent mode)

Source: Internet
Author: User

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)

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.