如何使用Golang实现访问者模式操作对象集合_增加额外功能而不修改对象
技术百科
P粉602998670
发布时间:2025-12-21
浏览: 次 Go中访问者模式通过接口解耦操作与数据结构:Element接口定义Accept方法,Visitor接口声明各类型Visit方法,新增功能只需实现新Visitor,无需修改原有结构体。
使用 Golang 实现访问者模式,核心在于将“操作逻辑”从“数据结构”中解耦。由于 Go 没有传统面向对象语言的继承与虚函数机制,我们借助接口、组合和类型断言来模拟访问者模式,实现对对象集合执行新操作(如打印、序列化、校验、统计)而无需修改原有结构体定义。
定义被访问的对象接口(Element)
让所有可被访问的结构体实现统一接口,该接口只暴露一个 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),这是实现双分派的关键一步。
定义访问者接口(Visitor)
声明一个访问者接口,为每种可被访问的类型提供对应的方法。新增功能只需实现这个接口的新版本,不碰原有结构体:
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+)辅助构建类型安全的访问调度层。
# ai
# 这是
# 已有
# 都要
# 只需
# 三步
# 新版本
# 数据结构
# 工具
# go
# golang
# 对象
# 泛型
# 接口
# 结构体
# 继承
# 切片
# 虚函数
# 遍历
# 面向对象
# 补上
# 新增功能
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- Win10怎么创建桌面快捷方式 Win10为应用创
- Python包结构设计_大型项目组织解析【指导】
- Mac怎么查看活动监视器_理解Mac进程和资源占用
- 如何使用Golang sync.Map实现并发安全
- Win11怎么开启专注模式_Windows11时钟
- Win11怎么查看wifi信号强度_检测Windo
- Win11怎么关闭定位服务 Win11禁止应用获取
- Python配置文件操作教程_JSONINIYAM
- 如何使用Golang处理静态文件缓存_提高页面加载
- How to Properly Use NumPy
- C++中的std::shared_from_thi
- windows如何禁用驱动程序强制签名_windo
- Win11怎么关闭应用权限_Windows11相机
- Windows10蓝屏SYSTEM_SERVICE
- Go 语言标准库为何不提供泛型 Contains
- C#如何序列化对象为XML XmlSerializ
- MAC怎么使用表情符号面板_MAC Emoji快捷
- 如何更改Windows资源管理器的默认启动位置?(
- 如何使用Golang实现文件加密_Golang c
- mac怎么查看wifi密码_MAC查看已连接WiF
- c++的static关键字有什么用 静态变量和静态
- Python日志系统设计与实现_高可观测性架构实战
- Win11怎么检查TPM2.0模块_Windows
- c++怎么用jemalloc c++替换默认内存分
- Windows10怎样设置家长控制_Windows
- Win10怎样安装Excel数据分析工具_Win1
- c++输入输出流 c++ cin与cout格式化输
- 如何在Golang中实现自定义Benchmark_
- VSC怎样在VSC中调试PHPAPI_接口调试技巧
- Win11怎么连接投影仪_Win11多显示器投屏设
- Win10怎么卸载爱奇艺_Win10彻底卸载爱奇艺
- Win10怎么卸载鲁大师_Win10彻底卸载鲁大师
- php增删改查在php8里有什么变化_新特性对cu
- Windows服务无法启动错误1067是什么_进程
- Windows10怎么查看硬件信息_Windows
- Win11怎么关闭系统声音_Win11系统提示音静
- php中$this和::能混用吗_对象与静态作用域
- Win11怎么清理C盘系统错误报告_Win11清理
- Windows10系统怎么查看系统版本_Win10
- php和redis连接超时怎么办_phpredis
- Win11如何设置文件权限 Win11 NTFS文
- Win11怎么查看显卡显存_查询Win11显卡详细
- 如何在Golang中处理模块包路径变化_Golan
- Mac如何开启夜览模式_Mac护眼模式设置与定时
- C++中的协变与逆变是什么?C++函数指针与返回类
- c++ std::atomic如何保证原子性 c+
- Mac如何调整Dock栏大小和位置_Mac程序坞个
- Windows系统时间服务错误_W32Time服务
- Win10如何卸载Skype_Win10卸载Sky
- Win10怎样卸载TeamViewer_Win10

)
QQ客服