Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
<a href="mailto:JBROWN2002@cfl.rr.com" rel="nofollow">JBROWN2002@cfl.rr.com</a>
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Stan James:
Hmmm, that's a C++ oriented article. Isn't it easier in Java?
In the package or module that houses the elements to be visited, define a visitor interface or base visitor class. Implement the interface or extend the base class in any number of other modules. The elements have no compile-time dependency on any concrete visitor.
For example I wrote some visitors to work with a DOM provided by another party by extending his base visitor. I know he didn't have any dependencies on my code!
BTW: Here's a scholarly definition of visitor: "The purpose of the Visitor Pattern is to encapsulate an operation that you want to perform on the elements of a data structure. In this way, you can change the operation being performed on a structure without the need of changing the classes of the elements that you are operating on." Leaves me scratchin my head.
I usually get more concrete - Visitor is a way to iterate over a set of objects without knowing exactly how they are put together. Might be a tree, might be a linked list, but either way the visitor is eventually visits all of em.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Stan James:
Ah, I think maybe I was looking at cyclic dependencies between packages and you were looking within. Is that right?
I guess I had chosen to live with the internal cycle.
Won't Element and the Visitor Interface always reference each other? Are there ways around that? (Not going to try to follow the C++ examples, sorry.) Is it a big deal?
My complaint with that other defintiion of visitor (forget where I found it) was that it was so abstract it meant nearly nothing. [...] The one from the inside cover of GoF is better: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation wtihout changing the classes of the elements on which it operates.
Here is some more about my experience with Visitor.
OptimalJ has a nice overview of Package Design principles. And a tool to measure how well you're doing with them.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I child proofed my house but they still get in. Distract them with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|