Chapter4:代理模式
代理模式应用场景:男生A代替男生B给漂亮女生C,送鲜花、巧克力、情书…
对于漂亮女生C只知道男生A的存在,并不知道B的存在。
但实际上B确实是存在的。
代理模式(Proxy)
为其他对象提供一种代理以控制对这个对象的访问。
代理模式的应用场景
第一:远程代理,也就是为一个对象在不同的地址空间提供局部代表。这样可以隐藏一个对象存在于不同地址空间的事实。
第二:虚拟代理,是根据需要创建开销很大的对象。通过它来存放实例化需要很长时间的真实对象。
第三:安全代理,用来控制真实对象访问时的权限。
第四:智能指针,是指当调用真实的对象时,代理处理另外一些事。
应用场景代码工程结构:
(1)IGiveGift.h 代理接口类
(2)Proxy.h 代理类
(3)Pursuit.h 追求者类
(4)BeautifulGirl.h 被追求者类
(5)ProxyApplication.cpp 客户端应用类
(1)IGiveGift.h 代理接口类
/************************************************************************/
/* 代理接口类 */
/************************************************************************/
#ifndef _IGIVEGIFT_H_
#define _IGIVEGIFT_H_
class IGiveGift
{
public:
virtual void GiveFlowers() = 0;
virtual void GiveChocolate() = 0;
virtual void GiveRing() = 0;
};
#endif //_IGIVEGIFT_H_
(2)Proxy.h 代理类
/************************************************************************/
/* 代理类,让“代理”也去实现“送礼物”接口 */
/************************************************************************/
#ifndef _PROXY_H_
#define _PROXY_H_
#include "IGiveGift.h"
#include "Pursuit.h"
class CProxy : public IGiveGift
{
public:
CProxy(CBeautifulGirl* pGirl)
: m_pHandsomeBoy(NULL)
{
m_pHandsomeBoy = new CPursuit(pGirl);
};
void GiveFlowers()
{
if (NULL != m_pHandsomeBoy)
{
m_pHandsomeBoy->GiveFlowers();
}
}
void GiveChocolate()
{
if (NULL != m_pHandsomeBoy)
{
m_pHandsomeBoy->GiveChocolate();
}
}
void GiveRing()
{
if (NULL != m_pHandsomeBoy)
{
m_pHandsomeBoy->GiveRing();
}
}
private:
CPursuit* m_pHandsomeBoy;
};
#endif //_PROXY_H_
(3)Pursuit.h 追求者类
/************************************************************************/
/* 追求者类,让“追求者”去实现“送礼物”接口 */
/************************************************************************/
#ifndef _PURSUIT_H_
#define _PURSUIT_H_
#include "BeautifulGirl.h"
class CPursuit : public IGiveGift
{
public:
CPursuit(CBeautifulGirl* pGirl)
{
m_pGirl = pGirl;
};
void GiveFlowers()
{
cout << GetName() << " 送你鲜花" << endl;
};
void GiveChocolate()
{
cout << GetName() << " 送你巧克力" << endl;
};
void GiveRing()
{
cout << GetName() << " 送你戒指" << endl;
};
private:
string GetName(void)
{
if (NULL != m_pGirl)
{
return (m_pGirl->GetName());
}
};
CBeautifulGirl* m_pGirl;
};
#endif //_PURSUIT_H_
(4)BeautifulGirl.h 被追求者类
/************************************************************************/
/* 被追求漂亮MM的类 */
/************************************************************************/
#ifndef _BEAUTIFUL_GIRL_H_
#define _BEAUTIFUL_GIRL_H_
#include <iostream>
#include <string>
using namespace std;
class CBeautifulGirl
{
public:
string GetName()
{
return m_strName;
};
void SetName(string strName)
{
m_strName = strName;
};
private:
string m_strName;
};
#endif //_BEAUTIFUL_GIRL_H_
(5)ProxyApplication.cpp 客户端应用类
// ProxyApplication.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Proxy.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
CBeautifulGirl* pGirl = NULL;
pGirl = new CBeautifulGirl();
if (NULL != pGirl)
{
pGirl->SetName("Beautiful girl");
}
CProxy* pProxyBoy = NULL;
pProxyBoy = new CProxy(pGirl);
if (NULL != pProxyBoy)
{
pProxyBoy->GiveFlowers();
pProxyBoy->GiveChocolate();
pProxyBoy->GiveRing();
}
Sleep(10000);
return 0;
}