jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Q Promoted Visitor pattern
Topic: Patterns
s.bala subramaniyam, Mar 25, 2001  [replies:3]
What is the Visitor pattern?

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Answer this question    Manage message; promote etc...  Hide so not publicly visible


Re: visitor pattern
Topic: Patterns
Alessandro A. Garbagnati PREMIUM, Mar 28, 2001
Hi,
A good answer to this question can be found at this link. It's really well written and the diagram it's very helpful to understand the simple logic behind this pattern.

In addition I think you can find useful even the JavaWorld's Tip 98: Reflect on the Visitor design pattern.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 1



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible
Re: Visitor pattern
Topic: Patterns
Sriram Gopalan, May 12, 2003  [replies:1]
A non-software answer would lie in the name of the pattern itself. It is a pattern to separate out the "members" of a place from the "visitors" to the place.

In the software world, the Visitor patterns helps you prevent code bloat in class hierarchies, typically. Consider a class hierarchy of parts. There are many subclasses of parts like resistor, capacitor etc. Initially, the classes have only their fundamental operations in their code. But, sooner or later, we want to do more operations like expose the part details in XML format, for example.

A naive approach would add the XML conversion operation to all the classes in the class hierarchy. If another such requirement comes about, we need to add those operations again in all the classes. This is painful for many reasons:

  1. The existing classes are stable and well-tested. Adding more methods by modifying them makes them unstable. All the testing has to be done again.
  2. There is code bloat in these classes and this affects the readability of the classes.
  3. The classes, now, no longer expose simple and orthogonal operations.

The visitor pattern alleviates this pain, by putting such non-fundamental operations in a single place outside the class hierarchy. The classes would be changed only once, to add an "accept()" method that accept an abstract visitor object. This method implementation would turn around and callback the "visitConcreteObject()" and pass itself (this) as the parameter. This is called double callback. For details of the implementation, refer to the GoF book.

In the above example, all the parts would have a method, say,

accept(AbstractVisitor avisitor)
. The implementation would be something like
public class Resistor {

  public void accept(AbstractVisitor avisitor) {
    avisitor.visitResistor(this);
  }
}

Now, the XML Outputter class will be a subclass of the AbstractVisitor and would like this:

public class XMLOutputVisitor extends AbstractVisitor {

  public void visitResistor(Resistor r) {
  ...
  }

  public void visitCapacitor(Capacitor c) {
  ... 
  }
 
  ...

}
and so on.

Please note that all the XMLOutput related code is in one place and if that requirement changes (to use SAX instead of DOM, for example), the code change is all in one place.

Also note that the visitor pattern is a good fit only if the the class hierarchy itself is stable. If a new type of part is added (say, a PCB) ALL the visitors have to change to add a method.

Hope this was helpful.

Is this item helpful?  yes  no     Previous votes   Yes: 1  No: 0



Reply to this answer/comment    Manage message; promote etc...  Take off my 'to do' list  Hide so not publicly visible

Re[2]: Visitor pattern
Topic: Patterns
Martin Hartnagel, Dec 22, 2005
Visitor pattern is commonly used for parsing. Writing a Parser for a defined semantic and syntax can be quite awesome. With a Parser Code Generator like the JavaCC Parser Generator with JJTree for example a code following the visitor pattern can be generated according to a defined grammar.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment    Manage message; promote etc...  Take off my 'to do' list  Hide so not publicly visible


Ask A Question



Related Links

Patterns FAQ

Patterns Forum

Design Patterns Home Page

Huston Design Patterns

Brad Appleton's Software Patterns Links

Object-Oriented Bibliography

Wish List
Features
About jGuru
Contact Us

 



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers