go-pattern-examples/behavior/07_visitor/visitor.go

60 lines
1.1 KiB
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package visitor
import "fmt"
2020-05-04 11:12:01 +03:00
////////////////////////////////
//使用石油的例子
2020-04-21 17:50:21 +03:00
2020-05-04 11:12:01 +03:00
//IGasResource 作为资源提供接口
type IGasResource interface {
Accept(IGasVisitor)
2020-04-21 17:50:21 +03:00
}
2020-05-04 11:12:01 +03:00
//gas 汽油
type gas struct {
density int
2020-04-21 17:50:21 +03:00
}
2020-05-04 11:12:01 +03:00
//IGasVisitor 访问者接口
type IGasVisitor interface {
Visit(gas)
2020-04-21 17:50:21 +03:00
}
2020-05-04 11:12:01 +03:00
//Accept 接待汽油客户
func (g gas) Accept(visitor IGasVisitor) {
visitor.Visit(g)
2020-04-21 17:50:21 +03:00
}
2020-05-04 11:12:01 +03:00
//diesel 柴油
type diesel struct {
energy int
2020-04-21 17:50:21 +03:00
}
2020-05-04 11:12:01 +03:00
//IDieselVisitor 访问者接口
type IDieselVisitor interface {
Visit(diesel)
2020-04-21 17:50:21 +03:00
}
2020-05-04 11:12:01 +03:00
//Accept 提供柴油
func (d diesel) Accept(visitor IDieselVisitor) {
visitor.Visit(d)
2020-04-21 17:50:21 +03:00
}
2020-05-04 11:12:01 +03:00
//militaryFactory 军工厂,消费石油,制造务器
type militaryFactory struct {
2020-04-21 17:50:21 +03:00
name string
}
2020-05-04 11:12:01 +03:00
//Visit 军工厂只够买柴油,制造武器
func (m *militaryFactory) Visit(d diesel) {
fmt.Println("militaryFactory: use diesel with inner energy", d.energy)
2020-04-21 17:50:21 +03:00
}
2020-05-04 11:12:01 +03:00
// clothFactory 服务装类工厂,购买汽油,制造化纤物品
type clothFactory struct{}
2020-04-21 17:50:21 +03:00
2020-05-04 11:12:01 +03:00
//Visit 购买汽油
func (c *clothFactory) Visit(g gas) {
fmt.Println("clothFactory: use gas with density", g.density)
2020-04-21 17:50:21 +03:00
}