-
Book Overview & Buying
-
Table Of Contents
TypeScript 5 Design Patterns and Best Practices - Second Edition
By :
The final pattern we will explore is the Visitor pattern. This pattern allows for the application of customized behavior to an existing collection of components that form a hierarchy, such as a tree or a linked list, without altering their structure or requiring them to implement an interface.
In practice, this involves adding a method to your components that accepts a reference to a Visitor object and passes its own instance as a parameter to this visitor. The Visitor object, in turn, gains access to each type of visited object’s public methods, enabling it to aggregate the state of each object it visits into a different result.
Next, we will explore more on when and how to use this pattern in more detail.
The Visitor pattern is particularly useful in several scenarios: