Join Jungwoo Ryoo for an in-depth discussion in this video Visitor example, part of Python: Design Patterns.
- So we'll start with the class that's being visited. The class is called House and we'll be defining the class. One of the main features of the House class is this method that triggers the visiting operation. The method is called accept and we'll be defining the method here. In fact, all it does is, getting this visitor instance and then invoke this visit method on this visitor instance.
So, the visitor instance is coming in as an argument. So we'll just type visitor . and then visit is the name of the method. And for the visitor to be able to visit, it needs a reference to this House class or the instance of the House class. That's why we type self here. So depending on the type of visitors, we'll be invoking either this method called work_on_hvac right here and this method is only called when the visitor is hvac_specialist and then we have another method called work_on_electricity and this method is only called by an electrician visitor.
So the work_on_hvac method is already defined. As you can see, all it does is displaying this message worked on by so and so and that will be, basically, the name of the object, in this case, the hvac_specialist object. So we'll be doing the same thing for this work_on_electricity method. So let's define what's being printed. So simply type print(self now "worked on by" is still the same.
Instead of hvac_specialist, now the visitor is electrician so that's why here we have to say electrician and one thing I want you to note is that we now have a reference to the electrician object in the house object because one of the arguments is a reference to the electrician object. The rest of this class is pretty straightforward and this string method is only returning what to print, especially when the object is being printed.
So in this case, we'll be simply printing the name of the object when it is printed by the print statement. Now let's move on to the Visitor class and this is an Abstract visitor meaning it's an Abstract class for the concrete visitors. So again all we do here is simply defining the string method where we decide on what to be displayed when this visitor object, especially the concrete objects, are being printed by the print statement.
And next is Hvac_Specialist class and one thing I want you to note is this inheritance relationship. So this is a concrete visitor and that's why it's inheriting from the Visitor class which is an Abstract visitor. And this class has one method called visit and all it does is really invoking this work_on_hvac method on the House obejct. So, again, I want you to note that the visitor now has a reference to the house object in this case because you're getting the house object as an argument here.
So another concrete visitor is this visitor called electrician and here, we do the same thing. So we haven't really finished the definition so we'll go ahead and finish it up. You just type house, a reference to the house object, and then invoke the method called work_on-- now, it's not hvac anymore because this is an electrician so we have to be calling a different method, in this case, electricity.
Work_on_electricity and then we pass the reference self. So we're done with the class definitions and let's now move on to the process of testing our classes. So first of all, we'll be creating an HVAC specialist, which is a concrete visitor. So all you have to do there is just give a variable, let's say hv assignment and we'll be instantiating the hvac specialist class so HvacSpecialist class is being instantiated and then let's also create an electrician.
So in this case, we'll use variable e assignment and then instantiate the electrician class. And then we need a house which is going to be worked on by either an electrician or HVAC specialist. So we'll name it home assignment basically we create a house object. And then the next step is letting the house actually accept the HVAC specialist and letting the HVAC specialist work on the house by invoking the visit method.
So all you have to do now here is invoking that accept method. So just type home and then accept and that's it. But we also need to pass our HVAC specialist object as an argument so that's where I'll do and let's run this and see what happens. Go to Tools, click on Build. So we have a little bit of typo here, let me go back and fix this. So the main thing is the indentation issue so I'm gonna just remove this space and remove this space and let's see what happens.
Let's try to run this again. Go to Tools and click on Build. And it works. So now, it displays this message, House worked on by HvacSpecialist because the visitor is HVAC Specialist. Now we'll try the same thing with the electrician and see what happens. So go to home accept this time we'll be accepting electrician which is represented by this variable e and we will run the program again. Go to Tools and click on Build.
And it works. House worked on by electrician. So the visitor object is very versatile in the sense that you can make it work on various elements of a class hierarchy and adding these new features to these existing class hierarchy is becoming very simple, like this.
- Understanding design patterns
- Best design practices: consistency, completeness, and correctness
- Working with creational patterns
- Working with structural patterns
- Working with behavioral patterns
Skill Level Intermediate
Q: In the strategy pattern example, why does the code keep executing the default function rather than the alternate?
A: The programming demonstration skips the step of defining what the strategy pattern should be when an alternate function name is provided
as an argument as shown below.