title: Visitor pattern implementation in java- How does this look?
body: <p>Alrite, I am gonna jump straight to the code:</p>
  
  <pre><code>public interface Visitor {
  
  public void visitInventory(); 
  public void visitMaxCount();
  public void visitCountry();
  public void visitSomethingElse();
  public void complete();
  //the idea of this visitor is that when a validator would visit it, it would validate data
  //when a persister visits it, it would persist data, etc, etc.
  // not sure if I making sense here...
  }
  
  public interface Visitable {
  public void accept(Visitor visitor); 
  }
  </code></pre>
  
  <p>here is a base implementation: </p>
  
  <pre><code>public class StoreValidator implements Visitor {
  private List &lt;ValidationError&gt; storeValidationErrors = new ArrayList&lt;ValidationError&gt;();
  
  public void addError(ValidationError error) {
  storeValidationErrors.add(error);
  }
  
  public List&lt;ValidationError&gt; getErrors() {
  return storeValidationErrors;
  }
  
  public void visitInventory() {
  // do nothing 
  }
  
  public void visitMaxCount() {
  //do nothing
  }
  //... etc..  all empty implementations 
  
  }
  </code></pre>
  
  <p>You will see why I did an empty implementation here... I would write a validator now.. which extends StoreValidator</p>
  
  <pre><code>public XYZValidator extends StoreValidator {
  
  @Override 
  public void visitInventory(Visitable visitable) { 
  // do something with visitable .. cast it to expected type
  // invoke a DAO, obtain results from DB
  // if errors found, do addError(new ValidationError()); with msg.
  }
  
  @Override 
  public void visitMaxCount(Visitable visitable) {
  //do something with visitable.. 
  }
  
  // I wouldn't implement the rest coz they wouldn't make sense
  // in XYZValidator.. so they are defined as empty in StoreValidator.
  
  }
  </code></pre>
  
  <p>Now here is what a visitable would look like:</p>
  
  <pre><code>public Store implements Visitable {
  
  public void accept(Visitor visitor) {
  visitor.visitInventory();
  visitor.visitMaxCount();
  }
  }
  </code></pre>
  
  <p>I could have code that does something like this on a list of Store objects:</p>
  
  <pre><code>List&lt;Store&gt; stores; //assume this has a list of stores.
  StoreValidator validator = new XYZValidator(); //or I would get it from a validatorfactory
  for(Store store: stores) {
             store.accept(validator); // so even if you send a wrong validator, you are good.
  }
  </code></pre>
  
  <p>Similarly you would have ABCValidator which would provide implementation for other methods (visitCountry / visitSomethinElse) and it would extend from StoreValidator. I would have another type of Object (not Store) defining accept method. </p>
  
  <p>I do see a problem here... 
  Say, I need a FileValidator which is different from StoreValidator, I would expect it to have none of these business related validations such as visitInventory(), etc. But, by having a single interface Visitor, I would endup declaring all kinds of methods in Visitor interface. Is that correct? Is this how you do it? </p>
  
  <p>I don't know if I got the pattern wrong, or if I am making any sense. 
  Please share your thoughts.</p>