设计模式之: 代理模式(proxy)

admin 2018-09-16 阅读


版权声明:copyright@xxdk2017 https://blog.csdn.net/u011583798/article/details/82721251

代理模式


代理模式的本质是在两个单向关联业务逻辑之间加入一个代理层,从而起到保护被代理目标,业务逻辑解耦,和功能拓展的目的。这种在计算机问题中加入中间层的解决问题哲学,在软硬件开发中屡见不鲜,比如软硬件的cache机制,智能指针,等;道不远人,下面我们用模拟基地组织塔利班向军火贩采购AK47的业务流程:

基地组织塔利班提出需要可以装备一个师的AK47 ==> 军火贩(代理) ==> 武器生产商 卡拉什尼科夫有限公司


C++实现

#include<iostream>

using namespace std;

class Weapon
{
public:
    Weapon(int id, std::string& name, std::string&  manufacturer)   
        : _id(id),
          _name(name),
          _manufacturer(manufacturer)
    {}
    void Info() const {
        std::cout << "Weapon manufacturer <<< "<<  _manufacturer <<" >>>";  
        std::cout << ", name <<< "<< _name << " >>>" << ", id <<< " << _id << " >>>" << std::endl;
    }
    virtual ~Weapon(){}

private:
    int _id;
    std::string _name;
    std::string _manufacturer;
};

class WeaponManufacturer 
{
public:
    WeaponManufacturer(std::string name, std::string address)
        : _name(name), 
          _address(address)
    {}

    virtual ~WeaponManufacturer(){}

    Weapon* ProduceWeapon(int id = 100, std::string weapon_name = "AK47") {
        std::cout << "5. WeaponManufacturer: Offer good quality weapons at low prices!\n";
        return new Weapon(id, weapon_name, _name);
    }

private:
    std::string _name;
    std::string _address;
};

class ArmsDealer
{
public:
    ArmsDealer(std::string name, WeaponManufacturer* pWm)
        : _name(name), _pWm(pWm) {}

    Weapon* DealArms(std::string& name) {
        std::cout <<"2. ArmsDealer: Professional weapons procurement!\n";
        return OfferWeaponToClient(name);
    }

private:
    std::string _name;
    WeaponManufacturer* _pWm;

    Weapon* OfferWeaponToClient(std::string& wp_name) {
        std::cout << "3. ArmsDealer: Offer weapon to client\n";
        return GetWeaponFromManufacturer(wp_name);      
    }

    Weapon* GetWeaponFromManufacturer(std::string& wp_name) {
        std::cout << "4. ArmsDealer: Get weapons from manufacturer.\n";
        return _pWm->ProduceWeapon(100, wp_name);
    }

};

class Al_Qaeda
{
public:
    Al_Qaeda(std::string name)
        : _name(name) {}

    Weapon* PurchaseRequest(ArmsDealer* pAd, std::string& name){
        std::cout << "1. Al_Qaeda: Need to be able to equip a division's AK47!\n";
        return pAd->DealArms(name);
    }

private:
    std::string _name;
};

int main()
{
    std::string name = "Kalashnikov Corporation";
    std::string address = "Российская Федерация";
    WeaponManufacturer* WM = new WeaponManufacturer(name, address);

    name = "WarDogs";
    ArmsDealer* AD = new ArmsDealer(name, WM);

    name = "Taliban";
    Al_Qaeda* AQ = new Al_Qaeda(name);

    name = "AK47";
    Weapon* ak47 = AQ->PurchaseRequest(AD, name);

    std::cout << "6. Al_Qaeda: ";
    ak47->Info();

    return 0;
}

C实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct _Weapon
{
    int _id;
    char _name[64];
    char _manufacturer[64];
}Weapon;

typedef struct _WeaponManufacturer
{
    Weapon* (*ProduceWeapon)(struct _WeaponManufacturer* pWm);
    char _name[64];
    char _addrress[64];
}WeaponManufacturer;

Weapon* ProduceWeapon(WeaponManufacturer* pWm)
{
    printf("5. WeaponManufacturer: Offer good quality weapons at low prices!\n");
    Weapon* wp = malloc(sizeof(Weapon));
    strncpy(wp->_name, "AK47", strlen("AK47")); 
    strncpy(wp->_manufacturer, pWm->_name, strlen(pWm->_name));
    wp->_id = 100;
    return wp;
}

typedef struct _ArmsDealer
{
    Weapon* (*DealArms)(struct _ArmsDealer* pAd);
    Weapon* (*GetWeaponFromManufacturer)(struct _ArmsDealer* pAd);
    Weapon* (*OfferWeaponToClient)(struct _ArmsDealer* pAd);
    char _name[64];
    //WeaponManufacturer *_wm;
    void* _wm;
}ArmsDealer;

typedef struct _Al_Qaeda
{
    Weapon* (*PurchaseRequest)(ArmsDealer* pA);
    char _name[64];
}Al_Qaeda;

Weapon* PurchaseRequest(ArmsDealer* pA)
{
    printf("1. Al_Qaeda: Need to be able to equip a division's AK47!\n");
    return pA->DealArms(pA);
}

Weapon* OfferWeaponToClient(ArmsDealer* pAd) 
{
    printf("3. ArmsDealer: Offer weapon to client\n");
    return pAd->GetWeaponFromManufacturer(pAd);
}

Weapon* GetWeaponFromManufacturer(ArmsDealer* pAd)
{
    printf("4. ArmsDealer: Get weapons from manufacturer.\n");
    return ((WeaponManufacturer*)(pAd->_wm))->ProduceWeapon((WeaponManufacturer*)pAd->_wm); 
}

Weapon* DealArms(ArmsDealer* pAd)
{
    printf("2. ArmsDealer: Professional weapons procurement!\n");
    return OfferWeaponToClient(pAd);
}

WeaponManufacturer KC = {
    ._name = "Kalashnikov Corporation", 
    ._addrress = "Российская Федерация",
    .ProduceWeapon = ProduceWeapon,
};

Al_Qaeda Taliban = {
    .PurchaseRequest = PurchaseRequest,
    ._name = "Tanliban",
};

ArmsDealer WarDogs = {
    .DealArms = DealArms,
    .GetWeaponFromManufacturer = GetWeaponFromManufacturer,
    .OfferWeaponToClient = OfferWeaponToClient,
    ._wm = &KC,
};

int main(int argc, const char* argv[])
{
    Weapon* ak47 = Taliban.PurchaseRequest(&WarDogs);
    printf("6. Al_Qaeda: weapon manufacturer <<< %s >>>, name <<< %s >>>, id <<< %d >>>\n",
            ak47->_manufacturer, ak47->_name, ak47->_id);

    return 0;
}

来源:未知

声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。