In this post, we’ll learn about the Visitor design pattern and it’s tradeoffs by looking at a real world example: Rubocop, a Ruby linting and formatting library.
The visitor design pattern separates the operation to be performed from a complex object structure (e.g. a tree whose nodes have many types).
This makes it easier to introduce new operations (“visitors”) for the same structure, at the cost that changing the object structure requires all visitors to be changed. So this pattern works best when changes to the underlying structure are rare, and we expect many different operations to be introduced.
Rubocop is a…