[Detailed Explanation of Visitor Design Pattern] C/Java/JS/Go/Python/TS Different Language Implementations

Introduction

Visitor Pattern (Visitor Pattern) is a behavioral pattern. It encapsulates a visitor class and collects the operations of each element class, with the purpose of separating the data structure from the data operation. Under the premise of not changing the data structure of the original element class, the execution algorithm of the element class is changed.

When some stable things (data structure or algorithm) do not want to be changed directly but want to extend the function, the visitor mode is suitable for this time. The frequency of use of the visitor pattern is not very high. In most cases, you don't need to use the visitor pattern, but when you need to use it, then you need to use it.

The visitor pattern has the following roles:

  • Structural object (ObjectStructure): Structural object role, which is the basic role of the visitor pattern, contains multiple classes or interfaces.
  • Abstract element (Element): define an accept access operation accept(), with a visitor Visitor as a parameter.
  • Concrete Element (ConcreteElement): realize the accept() method and processing operation of the abstract node, and call the access method of Vistor to realize the specific function.
  • Abstract visitor (Visitor): Define an abstract interface and declare one or more visit operations, so that all specific visitors must be implemented.
  • Specific visitor (ConcreteVisitor): The specific visitor role implements the interface declared by Visitor.

effect

  1. There is a method in the data foundation class to accept visitors and pass its own reference into the visitor, so as to fix the unchanged and open the changed.
  2. By isolating the things that change in the class and the things that are fixed, it conforms to the principle of single responsibility, and at the same time has good scalability and flexibility.

Implementation steps

  1. First create the basic element abstract class Element, and determine the accept() abstract method.
  2. Create several concrete element classes and implement the accept method of abstract elements.
  3. When creating the Visitor abstract interface, define the visit method and call the specific element.
  4. Create one or more Visitor classes, inherit the abstract interface, and customers will use this to visit specific elements.
  5. Then create the object structure class, which is the core entry class, responsible for combining various elements and passing the visitor.
  6. When the client calls, first create the object structure class, and then specify the visitor class, and call the specific element class by visiting this class

UML

visitor-pattern.png

java code

structure object

// ObjectStructure.java 结构对象(ObjectStructure)
public class ObjectStructure {

  // 可以想象为一台电脑,聚合了各种设备元素
  private String name = "Computer Structure";
  private List<Element> elements = new ArrayList<Element>();

  // 结构对象初始化聚合了其他元素
  public ObjectStructure() {
    addElement(new ConcreteElementA());
    addElement(new ConcreteElementB());
  }

  public void addElement(Element element) {
    elements.add(element);
  }

  // 传入访问者分发给其他元素
  public void accept(Visitor visitor) {
    System.out
        .println("ObjectStructure::accept() [visitor.class = " + visitor.getClass().getSimpleName() + " visitor.name = "
            + visitor.getName() + "]");
    for (Element element : elements) {
      element.accept(visitor);
    }
  }

  public String getName() {
    return this.name;
  }

}
复制代码

abstract visitor class

// Visitor.java 访问者Visitor抽象接口,定义不同的visit方法
public interface Visitor {
  public void visit(ConcreteElementA concreteElementA);

  public void visit(ConcreteElementB concreteElementB);

  public String getName();
}
复制代码

specific visitor

// ConcreteVisitorA.java 具体访问者A
public class ConcreteVisitorA implements Visitor {

  // 假如由不同厂商是程序的访问者
  private String name = "Google Visitor";

  @Override
  public void visit(ConcreteElementA concreteElementA) {
    System.out.println(
        "ConcreteVisitorA::visit() [Element.class = " + concreteElementA.getClass().getSimpleName()
            + " Element.name = "
            + concreteElementA.getName() + "]");
    concreteElementA.operate();
  }

  @Override
  public void visit(ConcreteElementB concreteElementB) {
    System.out.println("ConcreteVisitorA::visit() [Element.class = " + concreteElementB.getClass().getSimpleName()
        + " Element.name = "
        + concreteElementB.getName() + "]");
    concreteElementB.operate();
  }

  public String getName() {
    return this.name;
  }
}
复制代码
// ConcreteVisitorB.java 具体访问者B
public class ConcreteVisitorB implements Visitor {

  // 假如由不同厂商是程序的访问者
  private String name = "Apple Visitor";

  @Override
  public void visit(ConcreteElementA concreteElementA) {
    System.out.println(
        "ConcreteVisitorB::visit() [Element.class = " + concreteElementA.getClass().getSimpleName()
            + " Element.name = "
            + concreteElementA.getName() + "]");
    concreteElementA.operate();
  }

  @Override
  public void visit(ConcreteElementB concreteElementB) {
    System.out.println(
        "ConcreteVisitorB::visit() [Element.class = " + concreteElementB.getClass().getSimpleName()
            + " Element.name = "
            + concreteElementB.getName() + "]");
    concreteElementB.operate();
  }

  public String getName() {
    return this.name;

  }
}
复制代码

抽象元素类

// Element.java 抽象元素(Element),定义accept方法,传入抽象访问者
abstract class Element {
  public abstract void accept(Visitor visitor);
}
复制代码

具体元素实现类

// ConcreteElementA.java 具体的元素实现者A
public class ConcreteElementA extends Element {
  // 可以设想为显示器
  private String name = "Monitor Element";

  @Override
  public void accept(Visitor visitor) {
    System.out
        .println(
            "ConcreteElementA::accept() [visitor.class = " + visitor.getClass().getSimpleName() + " visitor.name = "
                + visitor.getName() + "]");
    visitor.visit(this);
  }

  public void operate() {
    System.out.println("ConcreteElementA::operate() [" + this.getName() + "]");
  }

  public String getName() {
    return this.name;
  }
}
复制代码
// ConcreteElementB.java 具体的元素实现者B
public class ConcreteElementB extends Element {
  private String name = "Keyboard Element";

  @Override
  public void accept(Visitor visitor) {
    System.out.println(
        "ConcreteElementB::accept() [visitor.class = " + visitor.getClass().getSimpleName() + " visitor.name = "
            + visitor.getName() + "]");
    visitor.visit(this);
  }

  public void operate() {
    System.out.println("ConcreteElementB::operate() [" + this.getName() + "]");
  }

  public String getName() {
    return this.name;
  }
}
复制代码

测试调用

  /**
   * 访问者模式是当客户需要访问具体各元素Element时,先建立一个访问者Visitor作为媒介
   * 客户基于对象结构ObjectStructure,调用accept(),接受传入的访问者
   * 对象结构向其他元素负责分发访问者,元素对象接受之后会将自己回传给访问者,从而访问者可以访问具体元素
   */
    ObjectStructure structure = new ObjectStructure();
    // 接受访问者A,把访问者传递给具体元素
    structure.accept(new ConcreteVisitorA());

    System.out.println("====");
    // 接受访问者B,把访问者传递给具体元素
    structure.accept(new ConcreteVisitorB());
复制代码

Go代码

结构对象

// ObjectStructure.go 结构对象(ObjectStructure)
type ObjectStructure struct {
  name     string
  elements []Element
}

func (o *ObjectStructure) AddElement(e Element) {
  o.elements = append(o.elements, e)
}

// 传入访问者分发给其他元素
func (o *ObjectStructure) Accept(v Visitor) {
  fmt.Println(
    "ObjectStructure::Accept() [Visitor.name = " +
      v.GetName() + "]")

  // 通知全部元素成员接受访问者
  for i := 0; i < len(o.elements); i++ {
    o.elements[i].Accept(v)
  }

  // for _, ele := range o.elements {
  //   ele.Accept(v)
  // }
}

func (o *ObjectStructure) GetName() string {
  o.name = "Computer Structure"
  return o.name
}

// 结构对象的初始化函数
func (o *ObjectStructure) Init() {
  // 可以想象为一台电脑,聚合了各种设备元素
  fmt.Println("ObjectStructure::Init() ", o.GetName())
  // 定义一个对象数组,长度可选
  o.elements = make([]Element, 0, 100)

  // 结构对象初始化聚合了其他元素
  o.AddElement(&ConcreteElementA{})
  o.AddElement(&ConcreteElementB{})
}
复制代码

抽象访问者类

// Visitor.go 访问者Visitor抽象接口,定义不同的visit方法
type Visitor interface {
  VisitA(e *ConcreteElementA)
  VisitB(e *ConcreteElementB)
  GetName() string
}
复制代码

具体访问者

// ConcreteVisitorA.go 具体访问者A
type ConcreteVisitorA struct {
  name string
}

func (v *ConcreteVisitorA) GetName() string {
  v.name = "Google Visitor(struct=ConcreteVisitorA)"
  return v.name
}

func (v *ConcreteVisitorA) VisitA(e *ConcreteElementA) {
  fmt.Println(
    "ConcreteVisitorA::VisitA() [Element.name = " + e.GetName() + "]")
  e.Operate()
}

func (v *ConcreteVisitorA) VisitB(e *ConcreteElementB) {
  fmt.Println(
    "ConcreteVisitorA::VisitB() [Element.name = " + e.GetName() + "]")
  e.Operate()
}
复制代码
// ConcreteVisitorB.go 具体访问者B
type ConcreteVisitorB struct {
  name string
}

func (v *ConcreteVisitorB) GetName() string {
  v.name = "Apple Visitor(struct=ConcreteVisitorB)"
  return v.name
}

func (v *ConcreteVisitorB) VisitB(e *ConcreteElementB) {
  fmt.Println(
    "ConcreteVisitorB::VisitB() [Element.name = " + e.GetName() + "]")
  e.Operate()
}

func (v *ConcreteVisitorB) VisitA(e *ConcreteElementA) {
  fmt.Println(
    "ConcreteVisitorB::VisitA() [Element.name = " + e.GetName() + "]")
  e.Operate()
}
复制代码

抽象元素类

// Element.go 抽象元素(Element),定义accept方法,传入抽象访问者
// go无抽象类,用interface替代
type Element interface {
  Accept(v Visitor)
  Operate()
  GetName() string
}
复制代码

具体元素实现类

// ConcreteElementA.go 具体的元素实现者A
type ConcreteElementA struct {
  name string
}

func (c *ConcreteElementA) GetName() string {
  c.name = `Monitor Element(struct=ConcreteElementA)`
  return c.name
}

func (e *ConcreteElementA) Accept(v Visitor) {
  fmt.Println(
    "ConcreteElementA::Accept() [Visitor.name = " + v.GetName() + "]")
  v.VisitA(e)
}

func (e *ConcreteElementA) Operate() {
  fmt.Println("ConcreteElementA::Operate() [" + e.GetName() + "]")
}
复制代码
// ConcreteElementB.go 具体的元素实现者B
type ConcreteElementB struct {
  name string
}

func (c *ConcreteElementB) GetName() string {
  c.name = "Keyboard Element(struct=ConcreteElementB)"
  return c.name
}

func (e *ConcreteElementB) Accept(v Visitor) {
  fmt.Println(
    "ConcreteElementB::Accept() [Visitor.name = " + v.GetName() + "]")
  v.VisitB(e)
}

func (e *ConcreteElementB) Operate() {
  fmt.Println("ConcreteElementB::Operate() [" + e.GetName() + "]")
}
复制代码

测试调用

func main() {
  fmt.Println("test start:")

  /**
   * 访问者模式是当客户需要访问具体各元素Element时,先建立一个访问者Visitor作为媒介
   * 客户基于对象结构ObjectStructure,调用Accept(),接受传入的访问者
   * 对象结构向其他元素负责分发访问者,元素对象接受之后会将自己回传给访问者,从而访问者可以访问具体元素
   */
  structure := src.ObjectStructure{}
  structure.Init()
  // 接受访问者A,把访问者传递给具体元素
  structure.Accept(&src.ConcreteVisitorA{})

  fmt.Println("====")
  // 接受访问者B,把访问者传递给具体元素
  structure.Accept(&src.ConcreteVisitorB{})
}
复制代码

更多语言版本

不同语言设计模式源码:github.com/microwind/d…

Guess you like

Origin juejin.im/post/7229144055832985656