One of the many cool things we learned last week was on how to traverse a collection using a visitor.
“…the visitor design pattern is a way of separating an algorithm from an object structure. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures.”
Let’s pretend that I’ve got an exam, and this exam has a bunch of questions and I want to find out how many questions have been completed. I could throw a method on my IExam type that returns the number of completed questions. Kind of like…
But what happens when I want to find out other statistics about the questions in my exam. I could add additional methods to the IExam type for each type of information that I want to query on, but this would totally violate the Open/Closed Principle. Let’s try to solve the same problem above using a visitor…
What this allows me to do is create new implementations of IVisitor’s that traverse the collection of questions but collect information that it needs. For example we could have a “CompletedQuestionsVisitor” that keeps track of the number of completed questions.
Now I can traverse the internal collection of questions and pick out the information that I need. What if I wanted to build a tree of specifications and wanted to traverse a collection of items and keep track of only those items that match my composite specification. Introducing the “Specification Visitor”.
Yes it’s a little overboard and slightly contrived, but perhaps someone can find a good home for him. Source Code (2.28MB)