Visitor Design Pattern Tutorial

Visitor Design Pattern TutorialWelcome to my Visitor Design Pattern Tutorial! This is the last part of my design pattern video tutorial.

The Visitor design pattern allows you to add methods to classes of different types without much altering to those classes. You can make completely different methods depending on the class used with this pattern.

With both the video below and the code that follows you should be able to start using this pattern in your code easily.

If you like videos like this, it helps if you tell Google with a click [googleplusone]

Share this video if you know someone who may like it [nttfblike]

Code from the Video

VISITOR.JAVA

TAXVISITOR.JAVA

VISITABLE.JAVA

LIQUOR.JAVA

NECESSITY.JAVA

TOBACCO.JAVA

TAXHOLIDAYVISITOR.JAVA

VISITORTEST.JAVA

37 thoughts on “Visitor Design Pattern Tutorial”

  1. Thank you so much for the video! You cleared up A LOT for me.

    I’m still stuck on one thing though…I still don’t understand what VISITABLE.JAVA does. I read the comments in the source code, but I still don’t get it 🙁

    1. You’re very welcome. Think of visitable as a way to sneak into the classes that implement it. It would be like if you knew someone hid a key to their house under their door mat. You can go in anytime you want. visitable places a method accept in the class. Then you’ll be able to call visitor.visit(this) when ever you like, which passes the class object that implements visitable with (this) to the visit method. Because I have 3 visit methods the right one is executed depending on whether a Liquor, Tobacco, or Necessity object is passed. Does that help. It is all about placing a method call in an object and then executing the right method using method overloading

  2. Dear Derek,
    Thanks for the effort that you are putting to create all these videos and articles which are very informative.
    Can you also consider creating articles/videos on Core J2EE Design Patterns which also would be very helpful for many, across the world?

    1. Thank you 🙂 Yes I will cover J2EE and the patterns specific to it and all of the frameworks as well. I’ll do my best to make videos as quickly as possible

  3. In the VisitorTest class you have called the appropriate methods by milk.accept(taxCalc) but that can be also called like taxCalc.visit(milk/vodka/cigars) or taxHolidayCalc .visit(milk/vodka/cigars) then what is the advantage here?

    ————————————–
    public class VisitorTest {
    public static void main(String[] args) {

    TaxVisitor taxCalc = new TaxVisitor();
    TaxHolidayVisitor taxHolidayCalc = new TaxHolidayVisitor();

    Necessity milk = new Necessity(3.47);
    Liquor vodka = new Liquor(11.99);
    Tobacco cigars = new Tobacco(19.99);

    System.out.println(milk.accept(taxCalc) + “\n”);
    System.out.println(vodka.accept(taxCalc) + “\n”);
    System.out.println(cigars.accept(taxCalc) + “\n”);

    System.out.println(“TAX HOLIDAY PRICES\n”);

    System.out.println(milk.accept(taxHolidayCalc) + “\n”);
    System.out.println(vodka.accept(taxHolidayCalc) + “\n”);
    System.out.println(cigars.accept(taxHolidayCalc) + “\n”);

    }
    }

  4. Hi Derek, thanks so much for all these videos! greatly helping me 🙂 is there a package with all source codes from the design pattern videos?

      1. Yes it did, I actually packaged all except one.

        If you ever want to add subtitles to your videos, I can provide Hebrew.

        Thanks again.

        1. Thank you for the subtitle offer. I’d do that if I could find an easy way to do it. I have been writing transcripts for all of my recent tutorials. I’m not sure if they help or not though?

  5. Hi Derek,

    I’ve been learning so much from these design patterns videos and thank you so much for it ! The content is just great !

    As for this visitor pattern example, the visit() and accept() methods have a ‘double’ return value. But doesn’t that restrict the kind of future-operations that could be added ?

    A few other examples that I went through have the visit() and accept() methods as void-methods and store the results in instance variables within the visitor. This second approach looks to be more flexible.

    So, If I were to add a DescriptionVisitor that could visit Liquor,Tobacco and Necessity classes, I wouldn’t have to add a method like:
    String take(Visitor)
    in each of those Visitable classes.

    Your thoughts ?

    Cheers,
    Vishwa

    1. Hi Vishwa,

      That is a very good point and I agree with you. The only reason why I locked them down and removed flexibility was for tutorial reasons. I have to constantly think about 2 things when I’m making these videos. First I need them to be accurate. Secondly I need them to be as easy to understand as possible.

      In my mind making something easier to understand normally forces me to make code that is less flexible. That is a shortcoming that I’m working on. That is why I constantly try to improve and I very much welcome great insights like you have provided here.

      Thank you for helping me make the tutorials better 🙂
      Derek

  6. Hi Derek,
    I went through couple of videos of design patterns and OO design.And as you said in one of the videos i absolutely agree with you saying nobody does this kind of videos on you tube.

    i have a request though if you can do TDD in similar lines of OO design, that would do world of good. As i thought you would be the best person to do such stuff once i saw your OO design videos. please take a use case / user story and if you can cover end – end like how you design and related thought process before you start with initial test. as i browsed a lot and see no body as ever provided such videos on TDD. every body say a problem and show the tests. but no body tell what is the design / thought process lead them to write such tests in front. hope i didn’t confused you.

    and thanks a ton for all you work.

    1. Hi Praveen,

      I did my best to explain test driven design in my Object Oriented Design tutorial. Of course it doesn’t really work unless you get a bunch of people in a room in front of a white board and work through the “Real World” process. I have been trying to get people to allow me to video tape that process. As I’m sure you know it mainly involves people throwing out ideas and then we run to our computers and work them out. Then back to the white board over and over again. It is a ton of fun if you are on a good team.

      I’ll do my best to get that on video. Thank you for the great request 🙂

        1. Sorry, but I’m not very good in regards to Indian cooking. I only know how to make a Green Curry with Eggplant. It is delicious to me, but I’m not sure if it is very accurate.

  7. Hi Derek,

    Great tutorials, thank you very much!

    Is there any reason why the Visitable isn’t an abstract class holding the visit method?

    public abstract class Visitable {
    public double accept(Visitor visitor) {
    return visitor.visit(this);
    }
    }

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *