鱼弦:CSDN内容合伙人、CSDN新星导师、全栈领域创作新星创作者 、51CTO(Top红人+专家博主) 、github开源爱好者(go-zero源码二次开发、游戏后端架构 https://github.com/Peakchen)
中介者模式原理详细解释:
中介者模式(Mediator Pattern)是一种行为型设计模式,用于降低多个对象之间的直接通信,并使其通过一个中介者对象进行交互。
中介者模式的核心思想是将多个对象之间的复杂交互逻辑集中在一个中介者对象中,从而使各个对象之间的耦合度降低。中介者对象充当了对象间的调度者和协调者,负责处理对象之间的通信和交互。
中介者模式通常包含以下几个角色:
- 中介者(Mediator):定义对象间的通信接口,并负责协调各个对象之间的交互。
- 具体中介者(Concrete Mediator):实现中介者接口,具体处理对象间的通信和交互逻辑。
- 同事类(Colleague):定义对象间通信的接口,维护一个对中介者对象的引用。
- 具体同事类(Concrete Colleague):实现同事类接口,通过中介者对象与其他同事类进行通信。
底层结构图:
以下是中介者模式的经典结构图:
+-----------------+ +-----------------+
| Mediator | | Colleague |
+-----------------+ +-----------------+
| +SendMessage() |<>------| +ReceiveMessage() |
+-----------------+ +-----------------+
▲
|
|
+-----------------+
| ConcreteMediator |
+-----------------+
| +colleague1 |
| +colleague2 |
| +colleague3 |
| +... |
+-----------------+
在上述结构图中,Mediator
是中介者的角色,定义了对象间的通信接口。它通常包含一个或多个方法,如 SendMessage()
,用于发送消息给其他同事类。
Colleague
是同事类的角色,定义了对象间通信的接口。它维护一个对中介者对象的引用,并通过调用中介者的方法来进行通信。
ConcreteMediator
是具体中介者的角色,实现了中介者接口,并具体处理对象间的通信和交互逻辑。它包含对各个同事类的引用,并根据需要调用各个同事类的方法。
使用场景解释:
中介者模式适用于以下场景:
-
当多个对象之间存在复杂的交互逻辑,导致对象之间的耦合度较高时,可以考虑使用中介者模式。中介者模式可以将复杂的交互逻辑集中在中介者对象中,使得各个对象之间的通信变得简单,并且减少了对象之间的直接依赖关系。
-
当需要通过一个中介者对象来协调和调度多个对象之间的交互时,可以使用中介者模式。中介者模式可以将对象间的通信集中管理,提供更好的可维护性和扩展性。
-
当希望通过增加或修改中介者对象来改变对象间的交互方式时,可以考虑使用中介者模式。中介者模式将对象间的交互逻辑封装在中介者对象中,因此可以通过修改中介者对象来改变对象间的交互方式,而无需修改各个同事类。
代码示例实现:
以下是一个使用Go语言实现中介者模式的示例:
package main
import "fmt"
// Mediator 中介者
type Mediator interface {
SendMessage(colleague Colleague, message string)
}
// Colleague 同事类
type Colleague interface {
ReceiveMessage(message string)
}
// ConcreteMediator 具体中介者
type ConcreteMediator struct {
colleague1 Colleague
colleague2 Colleague
}
func (m *ConcreteMediator) SendMessage(colleague Colleague, message string) {
if colleague == m.colleague1 {
m.colleague2.ReceiveMessage(message)
} else if colleague == m.colleague2 {
m.colleague1.ReceiveMessage(message)
}
}
// ConcreteColleague1 具体同事类1
type ConcreteColleague1 struct {
mediator Mediator
}
func (c *ConcreteColleague1) ReceiveMessage(message string) {
fmt.Println("ConcreteColleague1 received:", message)
}
func (c *ConcreteColleague1) SendMessage(message string) {
c.mediator.SendMessage(c, message)
}
// ConcreteColleague2 具体同事类2
type ConcreteColleague2 struct {
mediator Mediator
}
func (c *ConcreteColleague2) ReceiveMessage(message string) {
fmt.Println("ConcreteColleague2 received:", message)
}
func (c *ConcreteColleague2) SendMessage(message string) {
c.mediator.SendMessage(c, message)
}
func main() {
mediator := &ConcreteMediator{}
colleague1 := &ConcreteColleague1{
mediator: mediator,
}
colleague2 := &ConcreteColleague2{
mediator: mediator,
}
mediator.colleague1 = colleague1
mediator.colleague2 = colleague2
colleague1.SendMessage("Hello from ConcreteColleague1")
colleague2.SendMessage("Hello from ConcreteColleague2")
}
在上述示例中,我们定义了 Mediator
接口和 Colleague
接口,分别表示中介者和同事类的角色。然后,我们实现了具体的中介者 ConcreteMediator
和两个具体的同事类 ConcreteColleague1
和 ConcreteColleague2
。
在 ConcreteMediator
中,我们通过 SendMessage
方法来实现同事类之间的通信。在 ConcreteColleague1
和 ConcreteColleague2
中,我们分别实现了 ReceiveMessage
和 SendMessage
方法来接收和发送消息。
最后,在 main
函数中,我们创建了中介者对象和两个同事类对象,并将它们互相关联起来。然后,通过调用同事类的 SendMessage
方法来发送消息。
文献材料链接:
以下是一些关于中介者模式的文献和参考资料链接:
-
Design Patterns: Elements of Reusable Object-Oriented Software (GoF Book) - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
(《设计模式:可复用面向对象软件的基础》 - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) -
Mediator Pattern - SourceMaking
(中介者模式 - SourceMaking)
Mediator Design Pattern -
Mediator Design Pattern in Go - Tutorialspoint
(Go 中的中介者设计模式 - Tutorialspoint)
Design Patterns - Mediator Pattern | Tutorialspoint
当前都有哪些产品在使用:
中介者模式是一种常见的设计模式,在软件开发中被广泛应用。许多产品和框架在其内部或公开的API中使用了中介者模式来处理对象间的通信和交互。
以下是一些常见的产品和框架,它们可能使用了中介者模式或类似的设计模式:
-
GUI框架(如Java Swing、JavaFX、Qt等):这些框架通常使用中介者模式来协调界面组件之间的交互,例如按钮点击、菜单选择等。
-
消息中间件(如Apache Kafka、RabbitMQ等):这些消息中间件使用中介者模式来处理发布-订阅模型中的消息传递和交互。
-
聊天应用程序(如微信、Slack等):这些应用程序使用中介者模式来处理用户之间的聊天消息传递和交互。
-
游戏引擎(如Unity、Unreal Engine等