This repository has been archived on 2021-09-07. You can view files and clone it, but cannot push or open issues or pull requests.
2018-11-30 12:35:39 +08:00

60 lines
934 B
Go

package main
import (
"fmt"
)
type EnvExample struct {
Element
}
func (e EnvExample) Print(visitor IVisitor) {
e.Element.Accept(visitor)
}
func GetEnv() string {
return "testing"
}
// 定义访问者接口
type IVisitor interface {
Visit() // 访问者的访问方法
}
type ProductionVisitor struct {
}
func (v ProductionVisitor) Visit() {
fmt.Println("这是生产环境")
}
type TestingVisitor struct {
}
func (t TestingVisitor) Visit() {
fmt.Println("这是测试环境")
}
// 定义元素接口
type IElement interface {
Accept(visitor IVisitor)
}
type Element struct {
}
func (el Element) Accept(visitor IVisitor) {
visitor.Visit()
}
func main() {
// 创建一个元素
e := new(Element)
e.Accept(new(ProductionVisitor)) // output: 这是生产环境
e.Accept(new(TestingVisitor)) // output: 这是测试环境
m := new(EnvExample)
m.Print(new(ProductionVisitor))
m.Print(new(TestingVisitor))
}