23种设计模式C++实例之中介者模式

#include <iostream>

using namespace std;
/*
 * 中介者模式 
 * 定义一个中介对象来封装系列对象之间的交互
 * */

class Mediator;

class Person {
    public:
        Person(string name, int sex, int condition, Mediator* mediator) {
            this->name = name;
            this->sex = sex;
            this->condition = condition;
            this->mediator = mediator;
        }
        virtual ~Person() {}
        int getCondition() {
            return condition;
        }
        int getSex() {
            return sex;
        }
        string getName() {
            return name;
        }
        virtual void getParter(Person* p) = 0;
    protected:
        string name;
        int sex;
        int condition;
        Mediator* mediator;
};

class Mediator {
    public:
        void getParter() {
            if (man->getSex() == woman->getSex()) {
                cout << "I am not gay" << endl;  
                return;
            } 
            if (man->getCondition() == woman->getCondition()) {
                cout << man->getName() << "==" << woman->getName() << endl;
            } else {
                cout << man->getName() << "!=" << woman->getName() << endl;
            }
        } 
        void setMan(Person* man) {
            this->man = man; 
        }
        void setWoman(Person* woman) {
            this->woman = woman; 
        }
    private:
        Person* man;
        Person* woman;
};

class Woman: public Person {
    public:
        Woman(string name, int sex, int condition, Mediator* mediator): Person(name, sex, condition, mediator) {
        }
        int getCondition() {
            return condition;
        }
        virtual void getParter(Person* p) {
            mediator->setWoman(this);
            mediator->setMan(p);
            mediator->getParter();
        }
};

class Man: public Person {
    public:
        Man(string name, int sex, int condition, Mediator* mediator): Person(name, sex, condition, mediator) {
        }
        int getCondition() {
            return condition;
        }
        virtual void getParter(Person* p) {
            mediator->setMan(this);
            mediator->setWoman(p);
            mediator->getParter();
        }
};

int main() {
    Mediator* mediator = new Mediator;

    Person* woman1 = new Woman("woman1", 0, 5, mediator);   
    Person* woman2 = new Woman("woman2", 0, 5, mediator);   
    Person* man1 = new Man("man1", 1, 6, mediator);   
    Person* man2 = new Man("man2", 1, 5, mediator);   
    
    woman1->getParter(woman2);
    woman2->getParter(man1);
    woman2->getParter(man2);

    delete woman1;
    delete woman2;
    delete man1;
    delete man2;
    delete mediator;

    return 0; 
} 

GOF设计模式C++实例:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值