访问者模式通过接口和组合在Go中实现,将数据结构与操作分离,适用于文件系统遍历等需对稳定结构执行多操作的场景。

访问者模式是一种行为设计模式,它允许你在不修改对象结构的情况下定义新的操作。在 Go 语言中,虽然没有继承和重载支持,但通过接口和组合可以很好地实现访问者模式。
将数据结构与作用于其上的操作分离,使得可以在不改变结构的前提下添加新操作。这特别适用于需要对一组复杂对象结构执行多种不同操作的场景。
主要角色包括:
由于 Go 不支持方法重载,我们不能像 Java 那样为不同类型的元素定义同名但参数不同的 Visit 方法。解决方案是使用接口和类型断言,或通过统一的访问者接口配合类型判断。
立即学习“go语言免费学习笔记(深入)”;
示例:文件系统遍历
package main
import "fmt"
// Visitor 定义访问者接口
type Visitor interface {
VisitFile(*File)
VisitDirectory(*Directory)
}
// Element 定义可访问元素接口
type Element interface {
Accept(Visitor)
}
// File 文件类
type File struct {
Name string
Size int
}
func (f *File) Accept(v Visitor) {
v.VisitFile(f)
}
// Directory 目录类
type Directory struct {
Name string
Children []Element
}
func (d *Directory) Accept(v Visitor) {
v.VisitDirectory(d)
for _, child := range d.Children {
child.Accept(v)
}
}
// PrintVisitor 打印访问者
type PrintVisitor struct{}
func (v *PrintVisitor) VisitFile(f *File) {
fmt.Printf("文件: %s (%d字节)\n", f.Name, f.Size)
}
func (v *PrintVisitor) VisitDirectory(d *Directory) {
fmt.Printf("目录: %s\n", d.Name)
}
// SizeVisitor 统计大小访问者
type SizeVisitor struct {
TotalSize int
}
func (v *SizeVisitor) VisitFile(f *File) {
v.TotalSize += f.Size
}
func (v *SizeVisitor) VisitDirectory(d *Directory) {
// 目录本身不计入大小
}
func (v *SizeVisitor) GetTotal() int {
return v.TotalSize
}构建一个简单的文件树并应用不同的访问者:
func main() {
root := &Directory{
Name: "根目录",
Children: []Element{
&File{Name: "readme.txt", Size: 1024},
&Directory{
Name: "子目录",
Children: []Element{
&File{Name: "main.go", Size: 2048},
&File{Name: "config.json", Size: 512},
},
},
},
}
// 使用打印访问者
fmt.Println("=== 打印文件结构 ===")
printer := &PrintVisitor{}
root.Accept(printer)
// 使用统计访问者
fmt.Println("\n=== 计算总大小 ===")
sizeVisitor := &SizeVisitor{}
root.Accept(sizeVisitor)
fmt.Printf("总大小: %d 字节\n", sizeVisitor.GetTotal())
}Go 的访问者模式实现依赖接口和多态,关键在于合理设计 Element 和 Visitor 接口。
基本上就这些。Go 虽然语法简洁,但通过接口依然能优雅实现经典设计模式。访问者模式帮助你解耦数据与行为,让代码更具扩展性。
以上就是如何用Golang实现访问者模式_Golang 访问者模式实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号