中介者模式(大话设计模式)C/C++版本

中介者模式

C++

cpp 复制代码
// 中介者模式(Mediator),用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示得相互引用,从而使得其耦合松散,而且可以独立地改变他们之间得交互

#include <iostream>
#include <string>

using namespace std;

#define SAFE_DELETE(p) \
    if (p)             \
    {                  \
        delete p;      \
        p = NULL       \
    }

class Mediator;

// 抽象同事类
class Colleague
{
public:
    Colleague(Mediator *pMediator) : m_pMediator(pMediator){}; // 得到中介者对象
    virtual void Send(char *message) = 0;

protected:
    Mediator *m_pMediator;
};

// 具体同事1
class ConcreteColleague1 : public Colleague
{
public:
    ConcreteColleague1(Mediator *pMediator) : Colleague(pMediator){};

    virtual void Send(char *message);

    void Notify(char *message)
    {
        cout << "同事1得到消息:" << message << endl;
    }
};
// 具体同事2
class ConcreteColleague2 : public Colleague
{
public:
    ConcreteColleague2(Mediator *pMediator) : Colleague(pMediator){};

    virtual void Send(char *message);

    void Notify(char *message)
    {
        cout << "同事2得到消息:" << message << endl;
    }
};

// 主管
class Mediator
{
public:
    virtual void Send(char *message, Colleague *pColleague) = 0;
};

void ConcreteColleague1::Send(char *message)
{
    m_pMediator->Send(message, this);
}

void ConcreteColleague2::Send(char *message)
{
    m_pMediator->Send(message, this);
}
// 具体主管
class ConcreteMediator : public Mediator
{
public:
    // 重写声明方法 实现了两个对象得通信
    void Send(char *message, Colleague *pColleague)
    {
        ConcreteColleague1 *pConcreteColleague1 = dynamic_cast<ConcreteColleague1 *>(pColleague);

        if (pConcreteColleague1)
        {
            cout << "消息来自同事1" << endl;

            if (m_pColleague2)
            {
                m_pColleague2->Notify(message);
            }
        }
        else
        {
            cout << "消息来自同事2" << endl;
            if (m_pColleague1)
            {
                m_pColleague1->Notify(message);
            }
        }
    }

    // 具体主管了解改组下的所有得同事.... 所以拥有同事1和同事2得所有属性
    void setColleague1(Colleague *pColleague)
    {
        m_pColleague1 = dynamic_cast<ConcreteColleague1 *>(pColleague);
    }

    void setColleague2(Colleague *pColleague)
    {
        m_pColleague2 = dynamic_cast<ConcreteColleague2 *>(pColleague);
    }

private:
    ConcreteColleague1 *m_pColleague1; // 同事1
    ConcreteColleague2 *m_pColleague2; // 同事2
};

int main(int argc, char **argv)
{
    Mediator *pMediator = new ConcreteMediator();

    Colleague *pColleague1 = new ConcreteColleague1(pMediator);
    Colleague *pColleague2 = new ConcreteColleague2(pMediator);

    ConcreteMediator *pConcreteMediator = dynamic_cast<ConcreteMediator *>(pMediator);
    pConcreteMediator->setColleague1(pColleague1);
    pConcreteMediator->setColleague2(pColleague2);

    char message1[128] = "你吃饭了吗?";
    pColleague1->Send(message1);

    char message2[128] = "还没呢";
    pColleague2->Send(message2);

    delete pColleague1;
    delete pColleague2;
    delete pMediator;

    return 0;
}

C

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Mediator Mediator;
typedef struct Colleague Colleague;

// 定义抽象同事
typedef struct Colleague
{
    void (*send)(Colleague *self, char *message);
    void (*notify)(Colleague *self, char *message);
    Mediator *mediator;
} Colleague;

// 定义中介者
typedef struct Mediator
{
    void (*send)(Mediator *self, char *message, Colleague *pColleague);
    Colleague *colleague1;
    Colleague *colleague2;
} Mediator;

// 创建同事
Colleague *create_colleague(void (*send)(Colleague *self, char *message),
                            void (*notify)(Colleague *self, char *message))
{
    Colleague *colleague = malloc(sizeof(Colleague));
    colleague->send = send;
    colleague->notify = notify;
    colleague->mediator = NULL;
    return colleague;
}

// 创建中介者
Mediator *create_mediator()
{
    Mediator *mediator = malloc(sizeof(Mediator));
    mediator->send = NULL;
    mediator->colleague1 = NULL;
    mediator->colleague2 = NULL;
    return mediator;
}

// 具体同事1的发送函数
void concrete_colleague1_send(Colleague *self, char *message)
{
    self->mediator->send(self->mediator, message, self);
}

// 具体同事1的通知函数
void concrete_colleague1_notify(Colleague *self, char *message)
{
    printf("同事1得到消息:%s\n", message);
}

// 具体同事2的发送函数
void concrete_colleague2_send(Colleague *self, char *message)
{
    self->mediator->send(self->mediator, message, self);
}

// 具体同事2的通知函数
void concrete_colleague2_notify(Colleague *self, char *message)
{
    printf("同事2得到消息:%s\n", message);
}

// 具体中介者的发送函数
void concrete_mediator_send(Mediator *self, char *message, Colleague *pColleague)
{
    if (self->colleague1 == pColleague)
    {
        printf("消息来自同事1\n");
        if (self->colleague2 != NULL)
        {
            self->colleague2->notify(self->colleague2, message);
        }
    }
    else
    {
        printf("消息来自同事2\n");
        if (self->colleague1 != NULL)
        {
            self->colleague1->notify(self->colleague1, message);
        }
    }
}

int main()
{
    Mediator *mediator = create_mediator();
    mediator->send = concrete_mediator_send;

    Colleague *colleague1 = create_colleague(concrete_colleague1_send, concrete_colleague1_notify);
    Colleague *colleague2 = create_colleague(concrete_colleague2_send, concrete_colleague2_notify);

    colleague1->mediator = mediator;
    colleague2->mediator = mediator;

    mediator->colleague1 = colleague1;
    mediator->colleague2 = colleague2;

    char message1[] = "你吃饭了吗?";
    colleague1->send(colleague1, message1);

    char message2[] = "还没呢";
    colleague2->send(colleague2, message2);

    free(colleague1);
    free(colleague2);
    free(mediator);

    return 0;
}
相关推荐
马剑威(威哥爱编程)1 小时前
读写锁分离设计模式详解
java·设计模式·java-ee
修道-03231 小时前
【JAVA】二、设计模式之策略模式
java·设计模式·策略模式
iiiiiankor1 小时前
C/C++内存管理 | new的机制 | 重载自己的operator new
java·c语言·c++
小辛学西嘎嘎1 小时前
C/C++精品项目之图床共享云存储(3):网络缓冲区类和main
c语言·开发语言·c++
无敌最俊朗@2 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
2301_799084673 小时前
超全排序C语言实现
c语言·数据结构·算法·排序算法
G皮T4 小时前
【设计模式】结构型模式(四):组合模式、享元模式
java·设计模式·组合模式·享元模式·composite·flyweight
W_Meng_H4 小时前
设计模式-组合模式
设计模式·组合模式
六月悉茗11 小时前
【C语言 - 简易架构】
c语言·开发语言
風清掦12 小时前
C/C++每日一练:查找链表的中间节点
c语言·c++·链表