Go中访问者模式通过接口解耦操作与数据结构:Element接口定义Accept方法,Visitor接口声明各类型Visit方法,新增功能只需实现新Visitor,无需修改原有结构体。

使用 Golang 实现访问者模式,核心在于将“操作逻辑”从“数据结构”中解耦。由于 Go 没有传统面向对象语言的继承与虚函数机制,我们借助接口、组合和类型断言来模拟访问者模式,实现对对象集合执行新操作(如打印、序列化、校验、统计)而无需修改原有结构体定义。
让所有可被访问的结构体实现统一接口,该接口只暴露一个 Accept 方法,接收一个访问者:
type Element interface {
Accept(v Visitor)
}
type User struct {
Name string
Age int
Email string
}
func (u *User) Accept(v Visitor) {
v.VisitUser(u)
}
type Product struct {
ID int
Title string
Price float64
}
func (p *Product) Accept(v Visitor) {
v.VisitProduct(p)
}每个结构体自行决定调用访问者的哪个具体方法(如 VisitUser 或 VisitProduct),这是实现双分派的关键一步。
声明一个访问者接口,为每种可被访问的类型提供对应的方法。新增功能只需实现这个接口的新版本,不碰原有结构体:
立即学习“go语言免费学习笔记(深入)”;
type Visitor interface {
VisitUser(*User)
VisitProduct(*Product)
}
// 示例:打印访问者
type PrintVisitor struct{}
func (v *PrintVisitor) VisitUser(u *User) {
fmt.Printf("User: %s, %d years old, email: %s\n", u.Name, u.Age, u.Email)
}
func (v *PrintVisitor) VisitProduct(p *Product) {
fmt.Printf("Product: #%d %s ($%.2f)\n", p.ID, p.Title, p.Price)
}
// 新增功能?写个统计访问者,完全不用改 User/Product!
type CountVisitor struct {
UserCount, ProductCount int
}
func (v *CountVisitor) VisitUser(*User) { v.UserCount++ }
func (v *CountVisitor) VisitProduct(*Product) { v.ProductCount++ }把对象存进统一的 []Element 切片,然后遍历调用 Accept —— 具体执行哪个 VisitXxx 方法,由运行时对象类型和访问者实现共同决定:
items := []Element{
&User{Name: "Alice", Age: 30, Email: "a@example.com"},
&Product{ID: 101, Title: "Go Guide", Price: 29.99},
&User{Name: "Bob", Age: 25, Email: "b@example.com"},
}
// 使用打印访问者
printV := &PrintVisitor{}
for _, item := range items {
item.Accept(printV)
}
// 使用统计访问者
countV := &CountVisitor{}
for _, item := range items {
item.Accept(countV)
}
fmt.Printf("Found %d users, %d products\n", countV.UserCount, countV.ProductCount)如果未来新增了 Order 类型,只需三步:
Order 实现 Element 接口(加 Accept 方法)Visitor 接口中增加 VisitOrder(*Order)
若想避免每次新增类型都要改接口(违反开闭原则),可用空接口 + 类型断言做弱类型访问者,但会损失类型安全;更推荐配合代码生成工具(如 stringer 思路)或使用泛型约束(Go 1.18+)辅助构建类型安全的访问调度层。
以上就是如何使用Golang实现访问者模式操作对象集合_增加额外功能而不修改对象的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号