Need help make these classes use Visitor Pattern and generics
- by Shervin
Hi.
I need help to generify and implement the visitor pattern.
We are using tons of instanceof and it is a pain. I am sure it can be modified, but I am not sure how to do it.
Basically we have an interface ProcessData
public interface ProcessData {
  public setDelegate(Object delegate);
  public Object getDelegate();
  //I am sure these delegate methods can use generics somehow
}
Now we have a class ProcessDataGeneric that implements ProcessData
public class ProcessDataGeneric implements ProcessData {
  private Object delegate;
  public ProcessDataGeneric(Object delegate) {
    this.delegate = delegate;
  }
}
Now a new interface that retrieves the ProcessData
interface ProcessDataWrapper {
  public ProcessData unwrap();
}
Now a common abstract class that implements the wrapper so ProcessData can be retrieved
@XmlSeeAlso( { ProcessDataMotorferdsel.class,ProcessDataTilskudd.class })
public abstract class ProcessDataCommon implements ProcessDataWrapper {
  protected ProcessData unwrapped;
  public ProcessData unwrap() {
    return unwrapped;
  }
}
Now the implementation
public class ProcessDataMotorferdsel extends ProcessDataCommon {
  public ProcessDataMotorferdsel() {
    unwrapped = new ProcessDataGeneric(this);
  }
}
similarly
public class ProcessDataTilskudd extends ProcessDataCommon {
  public ProcessDataTilskudd() {
    unwrapped = new ProcessDataGeneric(this);
  }
}
Now when I use these classes, I always need to do instanceof
ProcessDataCommon pdc = null;
if(processData.getDelegate() instanceof ProcessDataMotorferdsel) {
   pdc = (ProcessDataMotorferdsel) processData.getDelegate();
} else if(processData.getDelegate() instanceof ProcessDataTilskudd) {
   pdc = (ProcessDataTilskudd) processData.getDelegate();
}
I know there is a better way to do this, but I have no idea how I can utilize Generics and the Visitor Pattern.
Any help is GREATLY appreciated.