设计模式代理模式(C++版)

一:代理模式简介

    专业版描述:为其他对象提供一种代理以控制对这个对象的访问。

    在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。

    戏剧性描述:房产市场不稳定,房价涨了还是涨。隔壁老王要买房,忍痛找到房产中介,中介把介绍给老王,老王买到中意房。

    这个剧情中老王是主角,演绎了一个买房的故事。房产中介是老王的代理,房子是老王的需求对象,由于中介有房产资源,老王又无法直接找到房源,只能找到中介去买房。

二:实践

    老王的故事在程序中得以实现。

   

    1.协议定义:买房协议(BuyRoomDelegate)。只有拥有买房协议才能成为房产中介。
    2.代理者:房产中介(RoomDelegate),他继承买房协议。

    3.需要代理者:买房人(BuyRoomPerson),买房需要找代理。

   
    编码如下:

   买房协议(BuyRoomDelegate)

#pragma once
#include <functional>

//函数定义:完成代理任务后给请求者反馈
typedef std::function<void(bool succeed, std::string msg)> RoomHandleCallback;


#pragma region BuyRoomDelegate

//买房协议
class BuyRoomDelegate
{
public:
    BuyRoomDelegate() {}
    ~BuyRoomDelegate() {}

public:
    //买房
    virtual bool TryBuy(std::string condition, RoomHandleCallback callback) = 0;

};
#pragma endregion

    房产中介(RoomDelegate):

#pragma region RoomDelegate

 //房产中介
 class RoomDelegate:public BuyRoomDelegate//, public RoomSellerDelegate
 {
 public:
     RoomDelegate() {}
     ~RoomDelegate() {}
 public:
     //实现父类接口
     //买房
     bool TryBuy(std::string condition, RoomHandleCallback callback);
    
     ////卖房
     // void Selling();
 private:

 };

#pragma endregion


//RoomDelegate.cpp
#include "stdafx.h"
#include "RoomDelegate.h"

bool RoomDelegate::TryBuy(std::string condition, RoomHandleCallback callback)
{
    cout << "中介找房:" << condition << endl;
    int a = rand() % 2;
    bool succeed = a == 0;//如果a==0,买房成功
    string msg = "";
    if (succeed)//成功
    {
        msg = "中介:恭喜啊";
        cout << "中介:终于卖出了一套房" << endl;
    }
    else
    {
        msg = "中介:对不住呀,这条件不好找呀!";
        cout << "中介:这条件太难找了。。。" << endl;
    }
    if (callback != nullptr)
    {
        callback(succeed, msg);
    }
    return succeed;
}

    买房人(BuyRoomPerson):

#pragma once

class BuyRoomDelegate;
//买房人
class BuyRoomPerson
{
public:
    BuyRoomPerson();
    ~BuyRoomPerson();

    //买房
    bool Buy(std::string condition);
public:
    //设置买房代理
    void setBuyRoomDelegate(BuyRoomDelegate* buyRoomDelegate) { m_buyRoomDelegate = buyRoomDelegate; }

private:
    //买房代理
    BuyRoomDelegate* m_buyRoomDelegate;
};

//BuyRoomPerson.cpp
#include "stdafx.h"

BuyRoomPerson::BuyRoomPerson()
{
    m_buyRoomDelegate = nullptr;
}

BuyRoomPerson::~BuyRoomPerson()
{
}

bool BuyRoomPerson::Buy(std::string condition)
{
    if (m_buyRoomDelegate != nullptr)
    {
        //string condition = "90万,90平,石家庄裕华区";
        //Lambda 表达式
        bool succeed = m_buyRoomDelegate->TryBuy(condition, [condition, this/*向表达式传参,临时参数、成员参数*/](bool succeed, std::string msg/*参数*/)->void/*返回值类型*/{
            if (succeed)
            {
                cout<< "买房成功" << condition <<endl;
            }
            else
            {
                cout << "没有谈妥,买房失败"<<condition << endl;
            }
        });
        return succeed;
    }
    return false;
}

    测试代码:

int main()
{
    //房产中介
    RoomDelegate* roomDel = new RoomDelegate();

    //买房人
    BuyRoomPerson* buyPer = new BuyRoomPerson();
    //买房人找个中介帮忙买房
    buyPer->setBuyRoomDelegate(roomDel);

    for (size_t i = 0; i < 10; i++)
    {
        //买房人向中介查询能否买到指定条件的房子
        bool succeed = buyPer->Buy("90万,90平,石家庄裕华区");
        cout << "=============================次数:"<<i<<endl;
        if (succeed)//如果买房成功了,就不买了
            break;
    }

    while (true)
    {
        char a;
        cin >> a;
    }
}

     输出结果:

中介找房:90万,90平,石家庄裕华区
中介:这条件太难找了。。。
没有谈妥,买房失败90万,90平,石家庄裕华区
=============================次数:0
中介找房:90万,90平,石家庄裕华区
中介:这条件太难找了。。。
没有谈妥,买房失败90万,90平,石家庄裕华区
=============================次数:1
中介找房:90万,90平,石家庄裕华区
中介:终于卖出了一套房
买房成功90万,90平,石家庄裕华区
=============================次数:2

    恭喜隔壁老王买到房了!!欢迎各位到老王家围观,哈哈哈。

原文地址:https://www.cnblogs.com/mengdongsky/p/6876668.html