© Dmitri Nesteruk 2020
D. NesterukDesign Patterns in .NET Core 3https://doi.org/10.1007/978-1-4842-6180-4_25

25. Visitor

Dmitri Nesteruk1  
(1)
St. Petersburg, c.St-Petersburg, Russia
 

To explain this pattern, I’m going to jump into an example first and then discuss the pattern itself. Hope you don’t mind!

Suppose you have parsed a mathematical expression (with the use of the Interpreter pattern, of course!) composed of double values and addition operators, for example:
(1.0 + (2.0 + 3.0))
This expression can be represented using an object hierarchy similar to the following:
public abstract class Expression { /* nothing here (yet) */ }
public class DoubleExpression : Expression
{
  private double value;
  public DoubleExpression(double value) { this.value ...

Get Design Patterns in .NET Core 3: Reusable Approaches in C# and F# for Object-Oriented Software Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.