-
Book Overview & Buying
-
Table Of Contents
Polished Ruby Programming - Second Edition
By :
The visitor pattern is most commonly used when you have many objects of separate classes that you need to handle. You have a visitor class that processes, or visits, each object and does something with it. Often when using the visitor pattern, you actually have multiple different types of operations that all need to deal with the same objects, so you have multiple visitor classes. However, you do not want to add methods for each visitor class to each of those separate classes. After all, while it is possible to define methods on any class in Ruby, it's considered bad practice to define methods on classes that are not part of your library, unless that is the sole purpose of your library.
The visitor pattern is a way around the problem of defining per-visitor methods in each class that is being visited. One approach to implementing the visitor pattern in Ruby is to have a single visit method that uses a case expression to dispatch each type of supported object to a...
Change the font size
Change margin width
Change background colour