中介者模式(Mediator Pattern)
# 中介者模式(Mediator Pattern)
# 概念
中介者模式是一种行为设计模式,它通过将对象之间的交互行为封装到一个中介者对象中,从而使对象之间的通信变得简单明了。中介者模式促进了松耦合,使对象之间的相互依赖减少,从而提高了系统的可维护性和可扩展性。
# 思想
中介者模式通过引入一个中介者对象,将系统中的对象之间的复杂交互关系转化为对象与中介者之间的简单交互。对象之间不再直接相互引用,而是通过中介者进行通信。当一个对象发生改变时,只需通知中介者,而无需直接与其他对象进行交互。中介者负责将通知传递给相关的对象,从而实现对象之间的协调与通信。
# 角色
- Mediator(中介者):定义了对象之间交互的接口,提供了向其他对象发送消息的方法。
- ConcreteMediator(具体中介者):实现了中介者接口,协调各个对象之间的交互关系。
- Colleague(同事类):定义了需要进行通信的对象的接口。
- ConcreteColleague(具体同事类):实现了同事类接口,与其他同事类通过中介者进行通信。
# 优点
- 减少了对象之间的直接依赖,降低了耦合度。
- 集中控制了对象之间的交互逻辑,使代码易于理解和维护。
- 可以简化对象之间的通信,提高系统的灵活性和扩展性。
# 缺点
- 中介者模式会增加一个中介者对象,可能会导致中介者本身变得复杂。
- 在某些情况下,中介者对象可能成为系统中的瓶颈,影响系统的性能。
# 类图
@startuml
class Mediator {
+Send(message: string, colleague: Colleague): void
}
class ConcreteMediator {
-colleague1: Colleague
-colleague2: Colleague
+SetColleague1(colleague: Colleague): void
+SetColleague2(colleague: Colleague): void
+Send(message: string, colleague: Colleague): void
}
class Colleague {
-mediator: Mediator
+Colleague(mediator: Mediator)
+Send(message: string): void
+Receive(message: string): void
}
class ConcreteColleague1 {
+Send(message: string): void
+Receive(message: string): void
}
class ConcreteColleague2 {
+Send(message: string): void
+Receive(message: string): void
}
ConcreteColleague1 --> Colleague
ConcreteColleague2 --> Colleague
Colleague "1..*" -- Mediator
ConcreteMediator "1" *- "2..*" Colleague
@enduml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 时序图
@startuml
actor Client
Client -> ConcreteMediator: SetColleague1(colleague)
Client -> ConcreteMediator: SetColleague2(colleague)
Client -> ConcreteColleague1: Send(message)
ConcreteColleague1 -> Mediator: Send(message, this)
Mediator -> ConcreteColleague2: Receive(message)
Client -> ConcreteColleague2: Send(message)
ConcreteColleague2 -> Mediator: Send(message, this)
Mediator -> ConcreteColleague1: Receive(message)
@enduml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 示例代码
下面是一个简单的示例代码,展示了中介者模式的使用。代码中有两个具体同事类 ConcreteColleague1
和 ConcreteColleague2
,它们通过中介者 ConcreteMediator
进行通信。当一个同事对象发送消息时,中介者将消息传递给其他同事对象。
#include <iostream>
#include <string>
class Colleague;
class Mediator {
private:
Colleague* colleague1;
Colleague* colleague2;
public:
void SetColleague1(Colleague* colleague);
void SetColleague2(Colleague* colleague);
void Send(const std::string& message, Colleague* colleague);
};
class Colleague {
protected:
Mediator* mediator;
public:
Colleague(Mediator* mediator) : mediator(mediator) {}
virtual void Send(const std::string& message) = 0;
virtual void Receive(const std::string& message) = 0;
};
void Mediator::SetColleague1(Colleague* colleague) {
colleague1 = colleague;
}
void Mediator::SetColleague2(Colleague* colleague) {
colleague2 = colleague;
}
void Mediator::Send(const std::string& message, Colleague* colleague) {
if (colleague == colleague1) {
colleague2->Receive(message);
} else if (colleague == colleague2) {
colleague1->Receive(message);
}
}
class ConcreteColleague1 : public Colleague {
public:
using Colleague::Colleague;
void Send(const std::string& message) override {
std::cout << "ConcreteColleague1 sends message: " << message << std::endl;
mediator->Send(message, this);
}
void Receive(const std::string& message) override {
std::cout << "ConcreteColleague1 receives message: " << message << std::endl;
}
};
class ConcreteColleague2 : public Colleague {
public:
using Colleague::Colleague;
void Send(const std::string& message) override {
std::cout << "ConcreteColleague2 sends message: " << message << std::endl;
mediator->Send(message, this);
}
void Receive(const std::string& message) override {
std::cout << "ConcreteColleague2 receives message: " << message << std::endl;
}
};
int main() {
Mediator mediator;
ConcreteColleague1 colleague1(&mediator);
ConcreteColleague2 colleague2(&mediator);
mediator.SetColleague1(&colleague1);
mediator.SetColleague2(&colleague2);
colleague1.Send("Hello from ConcreteColleague1");
colleague2.Send("Hello from ConcreteColleague2");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
代码运行结果:
ConcreteColleague1 sends message: Hello from ConcreteColleague1
ConcreteColleague2 receives message: Hello from ConcreteColleague1
ConcreteColleague2 sends message: Hello from ConcreteColleague2
ConcreteColleague1 receives message: Hello from ConcreteColleague2
1
2
3
4
2
3
4
在示例代码中,首先创建了一个中介者对象 mediator
,以及两个具体同事对象 colleague1
和 colleague2
。然后通过中介者对象设置同事对象的引用关系。当同事对象发送消息时,会调用中介者的 Send
方法,中介者根据发送者来决定将消息传递给哪个同事对象,然后同事对象通过 Receive
方法接收消息并进行处理。运行结果展示了两个具体同事对象之间的消息交互过程。
编辑 (opens new window)
上次更新: 2023/06/10, 08:05:09
- 01
- Linux系统移植(五)--- 制作、烧录镜像并启动Linux02-05
- 03
- Linux系统移植(三)--- Linux kernel移植02-05