note:wed_jun_25_2025

Typescript Cookbook

Bookmark: ch. 2.4

Visitor Pattern

To perform an operation on a pastry, we call its accept() method and pass in the visitor for the operation we want to execute. The pastry—the specific subclass’s overriding implementation of accept()—turns around and calls the appropriate visit method on the visitor and passes itself to it.

That’s the heart of the trick right there. It lets us use polymorphic dispatch on the pastry classes to select the appropriate method on the visitor class. In the table, each pastry class is a row, but if you look at all of the methods for a single visitor, they form a column.

  • note/wed_jun_25_2025.txt
  • Last modified: 2025/06/25 14:36
  • by lingao