Uncle Bob Martin on the Visitor Pattern

Some design patterns just appear weird the first time you come across them.  The “Visitor” pattern is like that for me because of all the double dispatch stuff.  I understand it and I use it on rare occasions, but I rarely reach for it because I’m not perfectly comfortable with it.  Uncle Bob of ObjectMentor has a great post on the Visitor pattern called “I Use Visitor” that’s a good explanation of the pattern.  Familiarity with a pattern like Visitor mitigates the weirdness.

It’s timely for me because I have a little side project going that will likely involve a “Composite” pattern that will include multiple operations across the aggregate object graph.  I’ll definitely consider a Visitor when I get there.

I used to grade code submissions from job applicants at a former employer.  One of the very few good submissions I ever looked at came from a C# coder who used a Visitor pattern on a sales tax calculation problem (think lot’s of exception cases).  At first blush I thought it was over-designed, but after a closer look I thought the usage of the Visitor pattern was a fairly elegant way to solve the problem.

About Jeremy Miller

Jeremy is the Chief Software Architect at Dovetail Software, the coolest ISV in Austin. Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy is the author of the open source StructureMap tool for Dependency Injection with .Net, StoryTeller for supercharged acceptance testing in .Net, and one of the principal developers behind FubuMVC. Jeremy's thoughts on all things software can be found at The Shade Tree Developer at http://codebetter.com/jeremymiller.
This entry was posted in Design Patterns. Bookmark the permalink. Follow any comments here with the RSS feed for this post.