码迷,mamicode.com
首页 > 其他好文 > 详细

代理模式

时间:2014-05-06 11:35:11      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   color   

代理模式:为其他对象提供一种代理以控制对这个对象的访问.

Pursuit 通过 Proxy 送给SchoolGirl 礼物.

 

bubuko.com,布布扣
#pragma once
class IGiveGift
{
public:
    virtual void GiveDolls()=0;
    virtual void GiveFlowers()=0;
    virtual void GiveChocolate()=0;
};
bubuko.com,布布扣

 

bubuko.com,布布扣
#pragma once
#include "igivegift.h"
#include "SchoolGirl.h"
#include "Pursuit.h"
class Proxy :
    public IGiveGift
{
public:
    Proxy(SchoolGirl mm)
    {
        GG = new Pursuit(mm);
    }
    ~Proxy(void);
    virtual void GiveDolls()
    {
        GG->GiveDolls();
    }
    virtual void GiveFlowers()
    {
        GG->GiveFlowers();
    }
    virtual void GiveChocolate()
    {
        GG->GiveChocolate();
    }
private:
    Pursuit *GG;
};
bubuko.com,布布扣

 

bubuko.com,布布扣
#pragma once
#include "Igivegift.h"
#include "SchoolGirl.h"
#include <iostream>
class Pursuit :
    public IGiveGift
{
public:
    Pursuit(SchoolGirl mn):mm(mn){}
    ~Pursuit(void);
    virtual void GiveDolls()
    {
        cout<<mm.getName()<<":送你洋娃娃"<<endl;
    }
    virtual void GiveFlowers()
    {
        cout<<mm.getName()<<":送你花朵"<<endl;
    }
    virtual void GiveChocolate()
    {
        cout<<mm.getName()<<":送你巧克力"<<endl;
    }
private:
    SchoolGirl mm;
};
bubuko.com,布布扣

 

bubuko.com,布布扣
#pragma once
#include <string>
using namespace std;
class SchoolGirl
{
public:
    SchoolGirl(string na):name(na){}
    string getName()
    {
        return name;
    }
    ~SchoolGirl(void);
private:
    string name;
};
bubuko.com,布布扣

 

main方法

bubuko.com,布布扣
#include "stdafx.h"
#include "Proxy.h"
#include "SchoolGirl.h"


int _tmain(int argc, _TCHAR* argv[])
{
    SchoolGirl girl("孙晓红");
    Proxy daili(girl);
    daili.GiveChocolate();
    daili.GiveDolls();
    daili.GiveFlowers();
    return 0;
}
bubuko.com,布布扣

 

代理模式,布布扣,bubuko.com

代理模式

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/crazycodehzp/p/3710631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!