国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++設(shè)計模式之Proxy模式(代理模式)詳解

2020-05-23 13:29:38
字體:
供稿:網(wǎng)友

代理模式很容易理解,就是代替別人去做某一件事,打個比方,我們需要買水果,一般是去超市或者水果店買水果,很少有人去果園買水果,果園是生產(chǎn)水果的地方,但很少出售水果,在這里,水果店,超市就成了代理。

首先定義一個抽象類,提供所有的函數(shù)接口。

1.定義賣水果的抽象類,也就是接口,果園與超市都要繼承這個類。

#pragma onceclass CSellFruits//定義一個抽象類{public: CSellFruits(void); virtual ~CSellFruits(void); virtual void sellapple()=0; //定義接口,賣蘋果 virtual void sellorange()=0;//定義接口,賣橘子}; #include "SellFruits.h"CSellFruits::CSellFruits(void){}  CSellFruits::~CSellFruits(void){}

2.定義具體類,也就是果園類,果園生產(chǎn)水果,但是一般不買水果

#pragma once#include "sellfruits.h"#include <stdio.h>class COrchard : public 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.定義代理類,代理賣水果的類

#pragma once#include "sellfruits.h"#include "Orchard.h"#include <stdio.h>class CProcySellFruits : public CSellFruits{public: CProcySellFruits(void); virtual ~CProcySellFruits(void); virtual void sellapple(); virtual void sellorange();private: CSellFruits *p_SellFruits; //傳入接口對象}; #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(); //用被代理的類實例化 } this->p_SellFruits->sellapple();//代理果園賣蘋果} void CProcySellFruits::sellorange(){ if(this->p_SellFruits==NULL) { this->p_SellFruits=new COrchard(); //用被代理的類實例化 } this->p_SellFruits->sellorange();//代理果園賣橘子}

4.實際調(diào)用

CProxySellFruits* p=new CProxySellFruits(); //用代理類賣水果 p->SellApple(); p->SellOrange();

 以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安徽省| 突泉县| 喀喇沁旗| 青河县| 怀来县| 黑山县| 福泉市| 巴马| 赤峰市| 鲁山县| 松阳县| 句容市| 巴彦淖尔市| 高阳县| 昭觉县| 西畴县| 苏州市| 常宁市| 赣榆县| 郸城县| 临湘市| 永仁县| 醴陵市| 新闻| 灯塔市| 黑龙江省| 金堂县| 景洪市| 班玛县| 福泉市| 永城市| 蒙城县| 新宾| 达州市| 兰考县| 屏东市| 宜兴市| 马尔康县| 安吉县| 寻甸| 沙湾县|