中介者模式通过引入中介者封装对象间交互,降低系统耦合度。在Golang中利用接口和组合实现,如聊天室示例中用户通过ChatRoom通信,避免直接引用,提升可维护性,适用于GUI联动、微服务协调等场景。

在复杂的系统中,多个对象之间直接通信会导致代码高度耦合、难以维护。中介者模式通过引入一个“中介者”来封装对象之间的交互逻辑,让对象不再互相引用,而是通过中介者进行通信。Golang 虽然没有类和继承,但凭借接口和组合机制,可以非常优雅地实现中介者模式。
中介者模式(Mediator Pattern)是一种行为设计模式,用于减少多个对象或组件之间的直接依赖。它将原本分散在多个对象中的交互逻辑集中到一个中介者对象中,从而降低系统的耦合度,提升可维护性。
典型应用场景包括:聊天室消息转发、GUI 组件联动、微服务间协调等。
在 Golang 中,我们通过接口来定义组件和中介者的行为契约,确保松耦合。
立即学习“go语言免费学习笔记(深入)”;
示例:模拟聊天室中用户之间的消息传递定义 Colleague(同事)接口:
type User interface {
Send(message string)
Receive(message, from string)
SetName(name string)
GetName() string
SetChatRoom(room ChatRoom)
}
定义 Mediator(中介者)接口:
type ChatRoom interface {
Register(user User)
Send(from, to, message string)
}
这样,所有用户只需知道中介者的存在,而不需要了解其他用户的细节。
接下来实现一个具体的聊天室作为中介者:
type SimpleChatRoom struct {
users map[string]User
}
func NewSimpleChatRoom() *SimpleChatRoom {
return &SimpleChatRoom{
users: make(map[string]User),
}
}
func (c *SimpleChatRoom) Register(user User) {
user.SetChatRoom(c)
c.users[user.GetName()] = user
c.Broadcast("system", fmt.Sprintf("%s 加入了聊天室", user.GetName()))
}
func (c *SimpleChatRoom) Send(from, to, message string) {
if to == "" {
c.Broadcast(from, message)
return
}
if user, exists := c.users[to]; exists {
user.Receive(message, from)
} else {
fmt.Printf("[系统] 用户 %s 不存在\n", to)
}
}
func (c *SimpleChatRoom) Broadcast(from, message string) {
for _, user := range c.users {
if user.GetName() != from {
user.Receive(message, from)
}
}
}
再实现一个具体用户:
type ChatUser struct {
name string
chatRoom ChatRoom
}
func NewChatUser(name string) *ChatUser {
return &ChatUser{name: name}
}
func (u *ChatUser) SetChatRoom(room ChatRoom) {
u.chatRoom = room
}
func (u *ChatUser) Send(message string) {
if u.chatRoom != nil {
u.chatRoom.Send(u.name, "", message) // 群发
}
}
func (u *ChatUser) SendTo(to, message string) {
if u.chatRoom != nil {
u.chatRoom.Send(u.name, to, message) // 私聊
}
}
func (u *ChatUser) Receive(message, from string) {
fmt.Printf("[%s] 收到来自 %s 的消息: %s\n", u.name, from, message)
}
func (u *ChatUser) SetName(name string) {
oldName := u.name
u.name = name
if u.chatRoom != nil {
// 可通知系统用户名变更
}
}
func (u *ChatUser) GetName() string {
return u.name
}
现在我们可以轻松地构建一个解耦的聊天系统:
func main() {
chatRoom := NewSimpleChatRoom()
alice := NewChatUser("Alice")
bob := NewChatUser("Bob")
charlie := NewChatUser("Charlie")
chatRoom.Register(alice)
chatRoom.Register(bob)
chatRoom.Register(charlie)
alice.Send("大家好!")
bob.SendTo("Charlie", "嘿,晚上一起写代码吗?")
charlie.Send("没问题!")
}
输出结果:
[系统] Alice 加入了聊天室 [系统] Bob 加入了聊天室 [系统] Charlie 加入了聊天室 [Bob] 收到来自 Alice 的消息: 大家好! [Charlie] 收到来自 Alice 的消息: 大家好! [Charlie] 收到来自 Bob 的消息: 嘿,晚上一起写代码吗? [Alice] 收到来自 Charlie 的消息: 没问题! [Bob] 收到来自 Charlie 的消息: 没问题!
可以看到,每个用户无需持有其他用户的引用,所有通信都由 ChatRoom 统一调度。
在 Golang 中使用中介者模式时,有几个关键点能提升代码质量:
基本上就这些。中介者模式在 Golang 中虽无显式类支持,但借助接口和结构体组合,依然能清晰表达设计意图,有效解耦复杂交互场景。关键是控制职责边界,保持中介逻辑简洁。不复杂但容易忽略。
以上就是如何使用Golang实现中介者模式解耦复杂交互_Golang 中介者模式实践技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号