代理(Proxy)模式
使用场景:使用代理来做事情;
#include "stdafx.h" #include<iostream> #include <string> using namespace std; class Girl { public: Girl(string name = ""):mName(name){} string getName() { return mName; } private: string mName; }; class GiveGift { public: virtual void GiveDolls() = 0; virtual void GiveFlowers() = 0; virtual void GiveChocolate() = 0; }; class Puisuit:public GiveGift { public: Puisuit(Girl mm):mGirl(mm){} virtual void GiveDolls() { cout<<"送"<<mGirl.getName()<<"玩具"<<endl; } virtual void GiveFlowers() { cout<<"送"<<mGirl.getName()<<"鲜花"<<endl; } virtual void GiveChocolate() { cout<<"送"<<mGirl.getName()<<"巧克力"<<endl; } private: Girl mGirl; }; class Proxy:public GiveGift { public: Proxy(Girl mm) { mPuisuit = new Puisuit(mm); } virtual void GiveDolls() { mPuisuit->GiveDolls(); } virtual void GiveFlowers() { mPuisuit->GiveFlowers(); } virtual void GiveChocolate() { mPuisuit->GiveChocolate(); } private: Puisuit* mPuisuit; }; int _tmain(int argc, _TCHAR* argv[]) { cout<<"代理模式"<<endl; Girl girl("娇娇"); Proxy pro(girl); pro.GiveDolls(); pro.GiveFlowers(); pro.GiveChocolate(); system("pause"); return 0; } 复制代码