Lecture thumbnail 0:00 / 0:00 We’re going to be discussing the visitor pattern now with real examples.
And the first example that I want to show you without using the visitor design pattern is the example
of printing very simple mathematical expressions.
So let’s imagine that we have mathematical expressions consisting solely of numbers and addition of
numbers.
So we may as well build some sort of hierarchy to define how these expressions all work.
So, for example, I can make an abstract class called expression, and here I will define a single
method public abstract void print, which is going to print the contents of the expression meaning the
textual representation of the expression into a Stringbuilder Stringbuilder.
SB There we go.
Okay, so now I’m going to inherit from expression and I’m going to have a double expression.
So this is an expression which is just going to keep a single double value that didn’t sound right,
a double value, and it’s going to keep it and print it as well.
So inherit from expression, we’re going to implement the print method.
But first of all, here is the actual value.
So we’ll have double value like so I’ll make a constructor to initialize this value.
And when it comes to printing the value, we’ll simply going to do SB dot append with the value.
Nothing particularly magical here.
Okay, next up, we’re going to have an addition expression.
So an addition expression is going to be an addition of two expressions itself.
Also being an expression.
There we go.
Once again, we implement the missing members, and here we’re going to have the left and right hand
sides of the addition.
And both of these are expressions.
This is why we have our abstract base class Now as an alternative to an abstract base class, you could
have had an interface.
It doesn’t really matter.
So here we’ll have expression left and right and once again I’m going to initialize them in the constructor
as soon as this completion begins to work.
There we go.
I’m going to get rid of these null checks and sort of merge them with assignments as well.
Okay, there we go.
So now to implement print here, we first of all append the opening round bracket.
Then we’re going to take the left part and we’re going to print it using the string builder.
Then we’re going to append the plus sign.
Then we’re going to once again print the right hand side.
So I’m going to say, right, dot print SB and then I will say SB Dot append and append the closing
bracket.
There we go.
Okay, So having made all of this, we can now try and make an expression and actually print this expression
somehow.
So let’s do this.
So we’re going to have the expression E I’m going to make a new addition expression here and the left
part of it will be a new double expression, double expression with the value of one.
And the right hand side is going to be a new addition expression.
So it’s an addition with an addition, with a double expression, with the value two and a double expression
with the value three.
There we go.
So this is how we’re going to define an expression, and then we’ll make a stringbuilder new stringbuilder
like so.
And then we’ll say expression dot print, and we’ll pass in the stringbuilder.
And then we will write line the contents of the string builder.
So let’s actually execute and see what we get.
In this case, hopefully we get the right output and here we go.
One plus two plus three with the brackets added correctly.
So how is all of this relevant to the visitor design pattern?
Well, here we’ve been very lucky in our ability to modify the base class and add the print method.
Now imagine a situation where you don’t have this option available.
You don’t have the print method inside the entire hierarchy, and you’ve got the expression hierarchy
already and you want to add the functionality to that hierarchy.
The question is how do you implement print if you don’t have access to the actual members?
And this is precisely what the visitor pattern lets you do.
So the visitor pattern is all about adding additional functionality when the hierarchies are already
set and you cannot go into the members themselves and modify them.
Play Stop Play Play Play Play Play Play Start Play information alert