Visitor pattern
A visitor pattern is a software design pattern that separates the algorithm from the object structure.
≈ 10 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
A visitor pattern is a software design pattern that separates the algorithm from the object structure. Because of this separation, new operations can be added to existing object structures without modifying the structures. It is one way to follow the open/closed principle in object-oriented programming and software engineering.
In essence, the visitor allows adding new virtual functions to a family of classes, without modifying the classes. Instead, a visitor class is created that implements all of the appropriate specializations of the virtual function. The visitor takes the instance reference as input, and implements the goal through double dispatch.
Programming languages with sum types and pattern matching obviate many of the benefits of the visitor pattern, as the visitor class is able to both easily branch on the type of the object and generate a compiler error if a new object type is defined which the visitor does not yet handle.
Overview
The Visitor design pattern is one of the twenty-three Gang of Four design patterns. namespace Wikipedia.Examples; using System; interface IVisitor { void Visit(Literal literal); void Visit(Addition addition); } class ExpressionPrintingVisitor : IVisitor { public void Visit(Literal literal) { Console.WriteLine(literal.Value); } public void Visit(Addition addition) { double leftValue = addition.Left.GetValue(); double rightValue = addition.Right.GetValue(); double sum = addition.GetValue(); Console.WriteLine($"{leftValue} + {rightValue} = {sum}"); } } abstract class Expression { public abstract void Accept(IVisitor visitor); public abstract double GetValue(); } class Literal : Expression { public Literal(double value) { this.Value = value; } public double Value { get; set; } public override void Accept(IVisitor visitor) { visitor.Visit(this); } public override double GetValue() { return Value; } } class Addition : Expression { public Addition(Expression left, Expression right) { Left = left; Right = right; } public Expression Left { get; set; } public Expression Right { get; set; } public override void Accept(IVisitor visitor) { Left.Accept(visitor); Right.Accept(visitor); visitor.Visit(this); } public override double GetValue() { return Left.GetValue() + Right.GetValue(); } } public static class Program { public static void Main(string[] args) { // Emulate 1 + 2 + 3 Addition e = new( new Addition( new Literal(1), new Literal(2) ), new Literal(3) ); ExpressionPrintingVisitor printingVisitor = new(); e.Accept(printingVisitor); Console.ReadKey(); } } "There's no syntax for creating a class. Classes are created by sending messages to other classes."
WriteStream subclass: #ExpressionPrinter instanceVariableNames: classVariableNames: package: 'Wikipedia'. ExpressionPrinter>>write: anObject "Delegates the action to the object. The object doesn't need to be of any special class; it only needs to be able to understand the message #putOn:" anObject putOn: self. ^ anObject.
Object subclass: #Expression instanceVariableNames: classVariableNames: package: 'Wikipedia'. Expression subclass: #Literal instanceVariableNames: 'value' classVariableNames: package: 'Wikipedia'. Literal class>>with: aValue "Class method for building an instance of the Literal class" ^ self new value: aValue; yourself.
Literal>>value: aValue "Setter for value" value := aValue. Literal>>putOn: aStream "A Literal object knows how to print itself" aStream nextPutAll: value asString. Expression subclass: #Addition instanceVariableNames: 'left right' classVariableNames: package: 'Wikipedia'.
Addition class>>left: a right: b "Class method for building an instance of the Addition class" ^ self new left: a; right: b; yourself. Addition>>left: anExpression "Setter for left" left := anExpression. Addition>>right: anExpression "Setter for right" right := anExpression.
Addition>>putOn: aStream "An Addition object knows how to print itself" aStream nextPut: $(. left putOn: aStream. aStream nextPut: $+. right putOn: aStream. aStream nextPut: $). Object subclass: #Program instanceVariableNames: classVariableNames: package: 'Wikipedia'.
Program>>main | expression stream | expression := Addition left: (Addition left: (Literal with: 1) right: (Literal with: 2)) right: (Literal with: 3). stream := ExpressionPrinter on: (String new: 100). stream write: expression. Transcript show: stream contents.
Transcript flush. type Visitor interface { visitWheel(wheel Wheel) string visitEngine(engine Engine) string visitBody(body Body) string visitCar(car Car) string } package org.wikipedia.examples; import java.util.List; interface CarElement { void accept(CarElementVisitor visitor); } interface CarElementVisitor { void visit(Body body); void visit(Car car); void visit(Engine engine); void visit(Wheel wheel); } class Wheel implements CarElement { private final String name; public Wheel(final String name) { this.name = name; } public String getName() { return name; } @Override public void accept(CarElementVisitor visitor) { /* * accept(CarElementVisitor) in Wheel implements * accept(CarElementVisitor) in CarElement, so the call * to accept is bound at run time. This can be considered * the *first* dispatch.
However, the decision to call * visit(Wheel) (as opposed to visit(Engine) etc.) can be * made during compile time since 'this' is known at compile * time to be a Wheel. Moreover, each implementation of * CarElementVisitor implements the visit(Wheel), which is * another decision that is made at run time.
This can be * considered the *second* dispatch. */ visitor.visit(this); } } class Body implements CarElement { @Override public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Engine implements CarElement { @Override public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Car implements CarElement { private final List elements; public Car() { this.elements = List.of( new Wheel("front left"), new Wheel("front right"), new Wheel("back left"), new Wheel("back right"), new Body(), new Engine() ); } @Override public void accept(CarElementVisitor visitor) { for (CarElement element : elements) { element.accept(visitor); } visitor.visit(this); } } class CarElementDoVisitor implements CarElementVisitor { @Override public void visit(Body body) { System.out.println("Moving my body"); } @Override public void visit(Car car) { System.out.println("Starting my car"); } @Override public void visit(Wheel wheel) { System.out.printf("Kicking my %s wheel%n", wheel.getName()); } @Override public void visit(Engine engine) { System.out.println("Starting my engine"); } } class CarElementPrintVisitor implements CarElementVisitor { @Override public void visit(Body body) { System.out.println("Visiting body"); } @Override public void visit(Car car) { System.out.println("Visiting car"); } @Override public void visit(Engine engine) { System.out.println("Visiting engine"); } @Override public void visit(Wheel wheel) { System.out.printf("Visiting %s wheel%n", wheel.getName()); } } public class VisitorDemo { public static void main(String[] args) { Car car = new Car(); car.accept(new CarElementPrintVisitor()); car.accept(new CarElementDoVisitor()); } } (defclass auto () ((elements :initarg :elements))) (defclass auto-part () ((name :initarg :name :initform " "))) (defmethod print-object ((p auto-part) stream) (print-object (slot-value p 'name) stream)) (defclass wheel (auto-part) ()) (defclass body (auto-part) ()) (defclass engine (auto-part) ()) (defgeneric traverse (function object other-object)) (defmethod traverse (function (a auto) other-object) (with-slots (elements) a (dolist (e elements) (funcall function e other-object)))) do-something visitations catch all (defmethod do-something (object other-object) (format t "don't know how ~s and ~s should interact~%" object other-object)) visitation involving wheel and integer (defmethod do-something ((object wheel) (other-object integer)) (format t "kicking wheel ~s ~s times~%" object other-object)) visitation involving wheel and symbol (defmethod do-something ((object wheel) (other-object symbol)) (format t "kicking wheel ~s symbolically using symbol ~s~%" object other-object)) (defmethod do-something ((object engine) (other-object integer)) (format t "starting engine ~s ~s times~%" object other-object)) (defmethod do-something ((object engine) (other-object symbol)) (format t "starting engine ~s symbolically using symbol ~s~%" object other-object)) (let ((a (make-instance 'auto :elements `(,(make-instance 'wheel :name "front-left-wheel") ,(make-instance 'wheel :name "front-right-wheel") ,(make-instance 'wheel :name "rear-left-wheel") ,(make-instance 'wheel :name "rear-right-wheel") ,(make-instance 'body :name "body") ,(make-instance 'engine :name "engine"))))) ;; traverse to print elements ;; stream *standard-output* plays the role of other-object here (traverse #'print a *standard-output*) (terpri) ;; print newline ;; traverse with arbitrary context from other object (traverse #'do-something a 42) ;; traverse with arbitrary context from other object (traverse #'do-something a 'abc)) (defmethod traverse (function (a auto)) ;; other-object removed (with-slots (elements) a (dolist (e elements) (funcall function e)))) ;; from here too ;; ... ;; alternative way to print-traverse (traverse (lambda (o) (print o *standard-output*)) a) ;; alternative way to do-something with ;; elements of a and integer 42 (traverse (lambda (o) (do-something o 42)) a) Now, the multiple dispatch occurs in the call issued from the body of the anonymous function, and so traverse is just a mapping function that distributes a function application over the elements of an object. Thus all traces of the Visitor Pattern disappear, except for the mapping function, in which there is no evidence of two objects being involved.
All knowledge of there being two objects and a dispatch on their types is in the lambda function. """ Visitor pattern example.
""" from abc import ABCMeta, abstractmethod from typing import NoReturn NOT_IMPLEMENTED: str = "You should implement this." class CarElement(metaclass = ABCMeta): @abstractmethod def accept(self, visitor: CarElementVisitor) -> NoReturn: raise NotImplementedError(NOT_IMPLEMENTED) class Body(CarElement): def accept(self, visitor: CarElementVisitor) -> None: visitor.visit_body(self) class Engine(CarElement): def accept(self, visitor: CarElementVisitor) -> None: visitor.visit_engine(self) class Wheel(CarElement): def __init__(self, name: str) -> None: self.name = name def accept(self, visitor: CarElementVisitor) -> None: visitor.visit_wheel(self) class Car(CarElement): def __init__(self) -> None: self.elements: list[CarElement] = [ Wheel("front left"), Wheel("front right"), Wheel("back left"), Wheel("back right"), Body(), Engine() ] def accept(self, visitor): for element in self.elements: element.accept(visitor) visitor.visit_car(self) class CarElementVisitor(metaclass = ABCMeta): @abstractmethod def visit_body(self, element: CarElement) -> NoReturn: raise NotImplementedError(NOT_IMPLEMENTED) @abstractmethod def visit_engine(self, element: CarElement) -> NoReturn: raise NotImplementedError(NOT_IMPLEMENTED) @abstractmethod def visit_wheel(self, element: CarElement) -> NoReturn: raise NotImplementedError(NOT_IMPLEMENTED) @abstractmethod def visit_car(self, element: CarElement) -> NoReturn: raise NotImplementedError(NOT_IMPLEMENTED) class CarElementDoVisitor(CarElementVisitor): def visit_body(self, body: Body) -> None: print("Moving my body.") def visit_car(self, car: Car) -> None: print("Starting my car.") def visit_wheel(self, wheel: Wheel) -> None: print(f"Kicking my {wheel.name} wheel.") def visit_engine(self, engine: Engine) -> None: print("Starting my engine.") class CarElementPrintVisitor(CarElementVisitor): def visit_body(self, body: Body) -> None: print("Visiting body.") def visit_car(self, car: Car) -> None: print("Visiting car.") def visit_wheel(self, wheel: Wheel) -> None: print(f"Visiting {wheel.name} wheel.") def visit_engine(self, engine: Engine) -> None: print("Visiting engine.") if __name__ == "__main__": car: Car = Car() car.accept(CarElementPrintVisitor()) car.accept(CarElementDoVisitor()) Visiting front left wheel. Visiting front right wheel.
Visiting back left wheel. Visiting back right wheel. Visiting body.
Visiting engine. Visiting car. Kicking my front left wheel.
Kicking my front right wheel. Kicking my back left wheel. Kicking my back right wheel.
Moving my body. Starting my engine. Starting my car. class Visitable: def accept(self, visitor: Visitor) -> Any: lookup: str = f"visit_{self.__qualname__.replace(".", "_")}" return getattr(visitor, lookup)(self) One could extend this to iterate over the class's method resolution order if they would like to fall back on already-implemented classes.
They could also use the subclass hook feature to define the lookup in advance.
Text derived from https://en.wikipedia.org/w/index.php?oldid=1337492867 under CC BY-SA 4.0; modifications have been made.