Visitor Design Pattern

[ad_1]
Code from the Video:

Welcome 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 and the code above you should be able to start using this pattern in your code easily.


Posted

in

by

Tags:

Comments

36 responses to “Visitor Design Pattern”

  1. Shimon Balzera Avatar

    You're great, Thank for your good work.

  2. Simon Giorgio Avatar

    Thanks man, very useful!

  3. Ali Mizan Avatar

    Thank you for posting! This video made the visitor design pattern super easy to understand.

  4. Thangam Arumugam Avatar

    really you are doing great help…i learned much better…thank you very much for your great help..:)

  5. maurice swanenberg Avatar

    I followed a course called Object Orientation last semester , and to be honest, I've learned just as much about it watching and implementing your tutorials in the past 3 days as I did in the course which lasted like 4 months..

  6. Angela Keller Avatar

    Hey ya! Thx you soooo much for this video tutorial! you really covered all the design patterns i need to know for my exam next week.
    I really like the way you explain the programming stuff!!

  7. Tunc Turel Avatar

    Hi Derek,

    Since I didn't know about this pattern before my gut reaction was as follows:

    Make a class called Necessities and let Liquor, Cigar and Milk extend that.
    Make another class called Taxes and add GetTaxedPriceFor(Necessities necessities, Date date).
    This method would take a commodity and the time and returns the corresponding tax for that.

    This way I could make a call like the following: Taxes.GetTaxedPriceFor(Milk,22/5/2017);

    Now, here I understand that every time you need to change tax calculations and you want to add new Necessities to the switch you need to open Taxes class and place a bunch of code in there. Soon you'll be hitting so many lines… Oh my god!

    (Edit: As an addition perhaps it's even better to assign types to necessities so the switch doesn't grow out of proportion and can return the taxed value for all foods for example.)

    In the Visitor pattern however you'll need to expand the TaxVisitor to handle this situation instead. The more implementations of Necessities you have the more you'll need to handle in the TaxVisitor.

    So what is the benefit here? Why should I use the Visitor pattern instead? I understand that I'm missing the point here so I kindly ask for your help. (Or any of the commenters for that matter.)

  8. Ana Alejandra Ballesteros Fuentes Avatar

    hi! Derek thanks for the tutorial….
    but i have this error:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "3,47"
    can you help me! thanks

  9. Sebastian Kupis Avatar

    Could function pointers be substitute to this pattern?

  10. Christopher Barrett Avatar

    Very well explained. I heard before coming to this video that this was a potential solution to instanceof being used in code (which I have seen a lot of). I will try to implement it using the concepts you have passed across.

  11. Lim Pei Avatar

    the clearest video i have ever watched on visitor pattern

  12. Tushar Bende Avatar

    Thanks ..very nicely explained with a very simple example to understand.

  13. Kavindu Wijesuriya Avatar

    Thank you @Derek. Pretty much better than my lecturer.

  14. musicmaker7889 Avatar

    Great video. I do have one question: the accept-method returns a double. What if our visitor-class implements methods that return Strings or any other datatype, or the "accept"-method is passed another visitor which has different returning data types? Should we just return "Object" and cast?

  15. Hossam Ayman Avatar

    why can't we just say taxCalc.visit(milk)
    instead of milk.accept(taxCalc) and visitor.visit(this) ??

  16. Andreas Egger Avatar

    @Derek Banas
    You got rly nice turorial videos there!
    Wouldnt it be bether to use an abstract class instead of an Visitable interface ? (cuz you would save at least some of your code).

  17. CobraL0rd Avatar

    What is the shortcut you use @ 9:22 to bring up the entire word? Your design patterns playlist is fantastic. Thanks.

  18. Dragan Smoljan Avatar

    Why we need Visitable Interface, its working properly without it just fine o.O

  19. John Salib Avatar

    I truly appreciate your videos , but if you could add your code also to Github that would be even better

  20. Brian Tep Avatar

    Any plans on doing a J2EE video playlist?

  21. Dragan Smoljan Avatar

    14.7.2016 9:15 the exact time i started to study this pattern, for now I cant understand shiet but lets laugh at this comment when I master it 😀

  22. madhav charan Avatar

    Loved the video 🙂

  23. Robert Keazor Avatar

    Hey dude , awesome video. Your really good a explanations!! Although Im not sure why you removed the @Override ,in the first method you overrode lol. But never the less I like listening to your design patterns almost every morning before work, good refreshers and your a good speaker. Quick Question did you ever get into the android game path ? I would like to see that

  24. rytah01 Avatar

    really cool. thanks!

  25. dominic van hasselt Avatar

    When trying to run i get: Exception in thread "main" java.lang.NumberFormatException: For input string: "3,47"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at TaxVisitor.visit(TaxVisitor.java:25)
    at Necessity.accept(Necessity.java:13)
    at VisitorTest.main(VisitorTest.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

  26. Vijay Sridhara Avatar

    You are awesome.. This Visitor pattern, you could only clarify!

  27. Kaan Etüt Merkezi Avatar

    A pattern which seemed very difficult in the Gang of Four, became very clear thanks to your video!!! Love your style…

  28. Nikolai Allen Avatar

    Thanks for the tutorial, though I would like to ask one thing. What would be the benefit of implementing this pattern versus passing an object, eg. Milk, directly into the Visitors visit functions while keeping the overrides as-is? This seems a little roundabout to me

  29. MyFoenix Avatar

    thank you very much!

  30. Кирилл Ольховский Avatar

    Thanks, mate! Really good explanation on simple example!

  31. DEV_GINZ Avatar

    I have a project that has two startVisit and two endVisit. Each of which take in two seperate classes. startVisit(Class c1) endVisit(Class c2) and startVisit(Class2 c1) endVisit(Class2 c2). I am working within node trees, so that might be the differentce between your example and what I am working on. Can you help me understand the use of these better?

  32. Rukshan Perera Avatar

    Thank you…
    Clearly understood the concept watching this.

  33. Max Hoffmann Avatar

    this was extremly helpfull

  34. gg2000gg2000 Avatar

    Very helpful! Thank you

  35. Andrzej Niedziela Avatar

    Hi Derek,

    What about Object Pool, Private Class Data, and Null Object design patterns?

    BTW, great job! I love your tutorial. Although, you could work a bit on your UML diagrams to stick more to the convention. 😉

    Regards,
    Andrzej

Leave a Reply

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