国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 响水县| 莱西市| 郁南县| 平阳县| 白朗县| 汝城县| 内黄县| 碌曲县| 徐水县| 通城县| 南和县| 青河县| 宜州市| 体育| 高碑店市| 瑞安市| 剑阁县| 武功县| 南皮县| 瓮安县| 兴义市| 宿松县| 长岛县| 哈密市| 宜良县| 东源县| 泰和县| 滦南县| 扎兰屯市| 阳原县| 霍邱县| 广平县| 万山特区| 柳河县| 衡东县| 聂荣县| 定日县| 宜川县| 樟树市| 五台县| 湖南省|