中介模式: Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring to each other explicitly,and it lets you vary their interaction independently.(用一个中介对象封装系列对象交互, 中介者使各对象需要显示地相互作用, 从而使其耦合松散, 而且可以独立地改变它们之间的交互。 )

UML图:

代码实现

#include <stdio.h&gt;
#include <stdlib.h&gt;
#include <string.h>

// 中介者接口
typedef struct {
    void (*notify)(void*, char*);
} Mediator;

// 具体中介者类
typedef struct {
    Mediator mediator;
    char* name;
} ConcreteMediator;

void ConcreteMediator_notify(void* obj, char* message) {
    ConcreteMediator* self = (ConcreteMediator*)obj;
    printf("%s received message: %sn", self->name, message);
}

ConcreteMediator createConcreteMediator(char* name) {
    ConcreteMediator mediator;
    mediator.mediator.notify = ConcreteMediator_notify;
    mediator.name = malloc(strlen(name) + 1);
    strcpy(mediator.name, name);
    return mediator;
}

// 抽象同事typedef struct {
    void (*send)(void*, char*);
    void (*receive)(void*, char*);
} Colleague;

// 具体同事typedef struct {
    Colleague colleague;
    Mediator* mediator;
    char* name;
} ConcreteColleague;

void ConcreteColleague_send(void* obj, char* message) {
    ConcreteColleague* self = (ConcreteColleague*)obj;
    printf("%s sends message: %sn", self->name, message);
    self->mediator->notify(self->mediator, message);
}

void ConcreteColleague_receive(void* obj, char* message) {
    ConcreteColleague* self = (ConcreteColleague*)obj;
    printf("%s received message: %sn", self->name, message);
}

ConcreteColleague createConcreteColleague(char* name, Mediator* mediator) {
    ConcreteColleague colleague;
    colleague.colleague.send = ConcreteColleague_send;
    colleague.colleague.receive = ConcreteColleague_receive;
    colleague.mediator = mediator;
    colleague.name = malloc(strlen(name) + 1);
    strcpy(colleague.name, name);
    return colleague;
}

int main() {
    ConcreteMediator mediator = createConcreteMediator("Mediator");
    ConcreteColleague colleague1 = createConcreteColleague("Colleague1", &amp;mediator.mediator);
    ConcreteColleague colleague2 = createConcreteColleague("Colleague2", &amp;mediator.mediator);

    colleague1.colleague.send(&amp;colleague1, "Hello, Colleague2!");
    colleague2.colleague.send(&amp;colleague2, "Hi, Colleague1!");

    free(mediator.name);
    free(colleague1.name);
    free(colleague2.name);

    return 0;
}

在上面的示例代码中,定义了中介者接口Mediator和具体中介者类ConcreteMediator实现通知方法处理同事对象之间的交互。

同时还定义抽象同事Colleague和具体同事ConcreteColleague实现发送接收方法,并通过中介者对象来处理同事之间的交互。

main函数中,首先创建一个具体中介者对象mediator然后创建两个具体同事对象colleague1colleague2,并将中介者对象传入。

然后通过调用同事对象的发送方法来向另一个同事对象发送消息,中介者对象将处理这些消息最后输出结果

中介者模式的优点:

  1. 可以减少对象之间的直接依赖关系,使代码更加灵活,易于维护

  2. 可以集中控制对象之间的交互逻辑,使代码更加清晰简洁

  3. 可以降低系统复杂度提高系统可维护性可扩展性

中介者模式的缺点:

  1. 中介者对象需要处理多个对象之间的交互逻辑可能会导致中介者对象变得复杂

  2. 中介者对象的存在会增加系统的开销,因为需要维护额外的对象和逻辑。

适用场景

  1. 对象之间的交互逻辑比较复杂,存在大量的依赖关系

  2. 对象之间的交互逻辑经常发生变化,需要进行频繁的修改

  3. 系统的对象之间存在多对多的关系,难以维护

原文地址:https://blog.csdn.net/qq_32059343/article/details/134751476

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_48608.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注