C++中介者模式例子

中介者模式将多对多的交互简化为一对多的交互,引入一个中介者使其对象耦合松散,而且可以独立的改变它们之间的交互。
mediator.h

#include <vector>
class CPerson;
class CMediator
{
public:
    CMediator(){}
    virtual ~CMediator(){}
    void Register(CPerson* aoPerson);
    CPerson* FindPartner(CPerson* aoPerson);
private:
    std::vector<CPerson*> moVecBoyPerson;
    std::vector<CPerson*> moVecGirlPerson;
};


mediator.cpp

#include "mediator.h"
#include "person.h"
void CMediator::Register(CPerson* aoPerson)
{
    CBoy* lpoBoy = dynamic_cast<CBoy*>(aoPerson);
    if (lpoBoy != NULL)
    {
        moVecBoyPerson.push_back(aoPerson);
    }
    else
    {
        moVecGirlPerson.push_back(aoPerson);
    }
}

CPerson* CMediator::FindPartner(CPerson* aoPerson)
{
    CBoy* lpoBoy = dynamic_cast<CBoy*>(aoPerson);
    if (lpoBoy != NULL)
    {
        std::vector<CPerson*>::iterator loIter = moVecGirlPerson.begin();
        for (; loIter != moVecGirlPerson.end(); ++loIter)
        {
            if (aoPerson->GetCondition() == (*loIter)->GetCondition())
            {
                return (*loIter);
            }
        }
    }
    else
    {
        std::vector<CPerson*>::iterator loIter = moVecBoyPerson.begin();
        for (; loIter != moVecBoyPerson.end(); ++loIter)
        {
            if (aoPerson->GetCondition() == (*loIter)->GetCondition())
            {
                return (*loIter);
            }
        }
    }

    return NULL;
}

person.h

#include <string>
class CMediator;
class CPerson
{
public:
    CPerson(CMediator* apoMedia, std::string aoName, int aiCond):mpoMedia(apoMedia),moName(aoName),miCond(aiCond)
    {

    }
    virtual ~CPerson(){}
    virtual CPerson* FindPartner(CPerson* aoPerson) = 0;
    void SetConditon(int aiCond)
    {
        miCond = aiCond;
    }
    int GetCondition()
    {
        return miCond;
    }
    std::string GetName()
    {
        return moName;
    }
private:
    std::string moName;
    int miCond;
protected:
    CMediator* mpoMedia;
};

class CBoy: public CPerson
{
public:
    CBoy(CMediator* apoMedia, std::string aoName, int aiCond);
    virtual ~CBoy(){}
    CPerson* FindPartner(CPerson* aoPerson);
};

class CGirl: public CPerson
{
public:
    CGirl(CMediator* apoMedia, std::string aoName, int aiCond);
    virtual ~CGirl(){}
    CPerson* FindPartner(CPerson* aoPerson);
};


person.cpp

#include "person.h"
#include "mediator.h"
CBoy::CBoy(CMediator* apoMedia, std::string aoName, int aiCond):CPerson(apoMedia, aoName, aiCond)
{
    mpoMedia->Register(this);
}

CPerson* CBoy::FindPartner(CPerson* aoPerson)
{
    return mpoMedia->FindPartner(aoPerson);
}

CGirl::CGirl(CMediator* apoMedia, std::string aoName, int aiCond):CPerson(apoMedia, aoName, aiCond)
{
    mpoMedia->Register(this);
}
CPerson* CGirl::FindPartner(CPerson* aoPerson)
{
    return mpoMedia->FindPartner(aoPerson);
}

mediatorTest.cpp

#include "person.h"
#include "mediator.h"
#include <stdio.h>

int main(int argv, char* argc[])
{
    CMediator* lpoMedia = new CMediator();
    CBoy* lpoBoy = new CBoy(lpoMedia, "张三", 6);
    CGirl*lpoGirl1 = new CGirl(lpoMedia, "李沁", 7);
    CGirl*lpoGirl2 = new CGirl(lpoMedia, "高媛媛", 6);
    CPerson* lpoPer = lpoGirl1->FindPartner(lpoGirl1);
    if (lpoPer != NULL)
    {
        printf("李沁匹配到 name: %s\n", lpoPer->GetName().c_str());
    }
    lpoPer = lpoGirl2->FindPartner(lpoGirl2);
    {
        printf("高媛媛匹配到 name: %s\n", lpoPer->GetName().c_str());
    }
    delete lpoGirl2;
    delete lpoGirl1;
    delete lpoBoy;
    delete lpoMedia;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值