设计模式——访问者模式

2020-10-31  本文已影响0人  DevilRoshan

什么是访问者模式?

封装一些作用于某种数据结构的各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作。

实现

// 人的抽象类。只有一个“接受”的抽象方法,它是用来获得“状态”对象的。
type Person interface {
    Accept(action Action)
}

type Man struct {
}

func NewMan() *Man  {
    return &Man{}
}

func (this *Man) Accept(action Action) {
    action.GetManAction(*this)
}

type Woman struct {
}

func NewWoman() *Woman  {
    return &Woman{}
}

func (this *Woman) Accept(action Action) {
    action.GetWomanAction(*this)
}

// 反应,男人反应,女人反应
type Action interface {
    GetManAction(man Man)
    GetWomanAction(woman Woman)
}
// Action 的具体上实现,成功,失败
type Success struct {
}

func NewSuccess() *Success  {
    return &Success{}
}

func (this *Success) GetManAction(man Man) {
    fmt.Println("男人成功...")
}

func (this *Success) GetWomanAction(woman Woman) {
    fmt.Println("女人成功...")
}

type Failure struct {
}

func NewFailure() *Failure  {
    return &Failure{}
}

func (this *Failure) GetManAction(man Man) {
    fmt.Println("男人失败...")
}

func (this *Failure) GetWomanAction(woman Woman) {
    fmt.Println("女人失败...")
}

// 结构对象
type ObjectStructure struct {
    elements []Person
}

func NewObjectStructure() *ObjectStructure  {
    return &ObjectStructure{}
}

func (this *ObjectStructure) Attach(person Person) {
    this.elements = append(this.elements, person)
}

func (this *ObjectStructure) Detach(person Person) {
    for idx, value := range this.elements {
        if value == person {
            this.elements = append(this.elements[:idx], this.elements[idx+1])
        }
    }
}

func (this *ObjectStructure) Display(action Action) {
    for _, person := range this.elements {
        person.Accept(action)
    }
}
func TestNewObjectStructure(t *testing.T) {
    objectStructure := NewObjectStructure()
    objectStructure.Attach(NewMan())
    objectStructure.Attach(NewWoman())
    // 成功
    success := NewSuccess()
    objectStructure.Display(success)
    // 失败
    failure := NewFailure()
    objectStructure.Display(failure)
}
// === RUN   TestNewObjectStructure
// 男人成功...
// 女人成功...
// 男人失败...
// 女人失败...
// --- PASS: TestNewObjectStructure (0.00s)
// PASS

优点

缺点

使用场景

上一篇 下一篇

猜你喜欢

热点阅读