设计模式 - 模板模式
设计模式 - MVC 模式

设计模式 - 访问者模式

violet posted @ Jul 10, 2020 06:51:09 AM in 笔记 with tags Design Pattern Golang , 231 阅读

在访问者模式(Visitor Pattern)中,我们使用了一个访问者类,它改变了元素类的执行算法。通过这种方式,元素的执行算法可以随着访问者改变而改变。这种类型的设计模式属于行为型模式。根据模式,元素对象已接受访问者对象,这样访问者对象就可以处理元素对象上的操作。

意图:主要将数据结构与数据操作分离。

主要解决:稳定的数据结构和易变的操作耦合问题。

何时使用:需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免让这些操作"污染"这些对象的类,使用访问者模式将这些封装到类中。

如何解决:在被访问的类里面加一个对外提供接待访问者的接口。

关键代码:在数据基础类里面有一个方法接受访问者,将自身引用传入访问者。

应用实例:您在朋友家做客,您是访问者,朋友接受您的访问,您通过朋友的描述,然后对朋友的描述做出一个判断,这就是访问者模式。

优点: 1、符合单一职责原则。 2、优秀的扩展性。 3、灵活性。

缺点: 1、具体元素对访问者公布细节,违反了迪米特原则。 2、具体元素变更比较困难。 3、违反了依赖倒置原则,依赖了具体类,没有依赖抽象。

使用场景: 1、对象结构中对象对应的类很少改变,但经常需要在此对象结构上定义新的操作。 2、需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免让这些操作"污染"这些对象的类,也不希望在增加新操作时修改这些类。

注意事项:访问者可以对功能进行统一,可以做报表、UI、拦截器与过滤器。

 

package main

import "fmt"

type ComputerPart interface {
	Accept(ComputerPartVisitor)
}

type ComputerPartVisitor interface {
	Visit(ComputerPart)
}

type Keyboard struct{}

func (k *Keyboard) Accept(visitor ComputerPartVisitor) {
	visitor.Visit(k)
}

type Monitor struct{}

func (m *Monitor) Accept(visitor ComputerPartVisitor) {
	visitor.Visit(m)
}

type Mouse struct{}

func (m *Mouse) Accept(visitor ComputerPartVisitor) {
	visitor.Visit(m)
}

type Computer struct {
	ComputerParts []ComputerPart
}

func NewComputer() *Computer {
	return &Computer{
		ComputerParts: []ComputerPart{&Keyboard{}, &Monitor{}, &Mouse{}},
	}
}

func (c *Computer) Accept(visitor ComputerPartVisitor) {
	for _, p := range c.ComputerParts {
		p.Accept(visitor)
	}
	visitor.Visit(c)
}

type ComputerPartDisplayVisitor struct{}

func (c *ComputerPartDisplayVisitor) Visit(part ComputerPart) {
	switch part.(type) {
	case *Computer:
		fmt.Println("Displaying computer")
	case *Keyboard:
		fmt.Println("Displaying keyboard")
	case *Monitor:
		fmt.Println("Displaying monitor")
	case *Mouse:
		fmt.Println("Displaying mouse")
	}
}

func main() {
	computer := NewComputer()
	computer.Accept(&ComputerPartDisplayVisitor{})
}
charlly 说:
Jan 16, 2023 01:43:27 PM

The Visitor Pattern is one of the most useful design patterns in software development. It allows you to add new functionality to existing classes without having to modify the existing code. The Visitor Pattern is a How Often Should I Change the Oil in a Rolls Royce great way to keep your code clean and easy to maintain.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter